Reputation: 103
Good day again, so I have a form and a user can add multiple titles. Each submit will create a new object which includes the written title and the user who wrote it(submitted the form).
I tried it with this but I don't know what to add in NewTitle.objects.create(title=title, ...)
\views.py
def title_view(request):
try:
profile = request.user.newtitle
except NewTitle.DoesNotExist:
profile = NewTitle(user=request.user)
if request.method == 'POST':
form = NewTitleForm(request.POST, instance=profile)
if form.is_valid():
title = form.cleaned_data["title"]
NewTitle.objects.create(title=title) #in the () I have to add the user= aswell
return redirect('/another')
else:
form = NewTitleForm(instance=profile)
return render(request, 'test.html', {'form': form, 'profile': profile})
\models.py
class NewTitle(models.Model):
user = models.OneToOneField(
User, default=None, null=True, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
I am working with basic User model. Currently, a user can submit the form and a new object will create just with the given title but without the user who wrote it because I just added title=title and not user=...
Any ideas?
Upvotes: 1
Views: 3130
Reputation: 4991
There steps may help you in getting resolved this issue
Upvotes: 0
Reputation: 476557
If you want to update an existing NewTitle
for that user, or create a new one if no such item exists, you can .save()
the form
, so:
from django.contrib.auth.decorators import login_required
@login_required
def title_view(request):
try:
profile = request.user.newtitle
except NewTitle.DoesNotExist:
profile = NewTitle(user=request.user)
if request.method == 'POST':
form = NewTitleForm(request.POST, instance=profile)
if form.is_valid():
form.save()
return redirect('/another')
else:
form = NewTitleForm(instance=profile)
return render(request, 'test.html', {'form': form, 'profile': profile})
You should also unident the return render(…)
part such that if the form fails, you rerender the form with the error messages.
If you want to create a new one each time, you can not do that with a OneToOneField
: a OneToOneField
is a ForeignKey
with unique=True
: it thus means that the user has at most one NewTitle
object that refers to that user. If you want to be able to construct multiple ones, you thus can work with a ForeignKey
:
from django.conf import settings
class NewTitle(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
default=None,
null=True,
on_delete=models.CASCADE
)
title = models.CharField(max_length=200)
and then the view looks like:
from django.contrib.auth.decorators import login_required
@login_required
def title_view(request):
if request.method == 'POST':
form = NewTitleForm(request.POST, instance=NewTitle(user=request.user))
if form.is_valid():
form.save()
return redirect('/another')
else:
form = NewTitleForm()
return render(request, 'test.html', {'form': form})
but then you can of course not fetch the .newtitle
of a User
object, since there can be zero, one or multiple ones.
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.
Note: You can limit views to a view to authenticated users with the
@login_required
decorator [Django-doc].
Upvotes: 1