Django Formset Gets Everything from Database
Question:
I need to create a form that allows users to add rows.
models.py:
class SocialURL(models.Model):
user = models.ForeignKey('User', on_delete=models.SET_NULL, null=True)
platform= models.ForeignKey('Platform', on_delete=models.SET_NULL, null=True)
link = models.URLField(max_length=254, null=True, blank=True)
def __str__(self):
return f"{self.link}"
forms.py:
class LinkForm(forms.ModelForm):
class Meta:
model = SocialURL
fields = ['platform','link']
views.py:
@login_required
def my_formset_view(request):
activeuser = request.user.id
response = None
formset_class = modelformset_factory(
model=SocialURL, fields=('platform', 'link'), extra=0, can_delete=True)
if request.method == 'POST':
formset = formset_class(data=request.POST)
if formset.is_valid():
formset.save(commit=False)
formset.user_id = activeuser
formset.save()
response = redirect(to='profiles:home')
else:
formset = formset_class()
if response is None:
response = render(
request, 'profiles/social_form.html', dict(formset=formset))
return response
I tried a couple of solutions I found here. I created a formset and it’s working, however, it’s pulling everything I have in the database, even other users’ links.
If I click on submit, it saves all of these links for the active user as well. How can I make it pull only the active user’s data?
Answers:
probably you are not give correct Queryset
in init?
queryset=SocialURL.objects.filter(user_id = activeuser.id))
if request.method == 'POST':
formset = formset_class(data=request.POST, queryset=queryset)
...
else:
formset = formset_class(queryset=queryset)
...
More here: https://docs.djangoproject.com/en/4.0/ref/forms/models/#django.forms.models.modelformset_factory
I need to create a form that allows users to add rows.
models.py:
class SocialURL(models.Model):
user = models.ForeignKey('User', on_delete=models.SET_NULL, null=True)
platform= models.ForeignKey('Platform', on_delete=models.SET_NULL, null=True)
link = models.URLField(max_length=254, null=True, blank=True)
def __str__(self):
return f"{self.link}"
forms.py:
class LinkForm(forms.ModelForm):
class Meta:
model = SocialURL
fields = ['platform','link']
views.py:
@login_required
def my_formset_view(request):
activeuser = request.user.id
response = None
formset_class = modelformset_factory(
model=SocialURL, fields=('platform', 'link'), extra=0, can_delete=True)
if request.method == 'POST':
formset = formset_class(data=request.POST)
if formset.is_valid():
formset.save(commit=False)
formset.user_id = activeuser
formset.save()
response = redirect(to='profiles:home')
else:
formset = formset_class()
if response is None:
response = render(
request, 'profiles/social_form.html', dict(formset=formset))
return response
I tried a couple of solutions I found here. I created a formset and it’s working, however, it’s pulling everything I have in the database, even other users’ links.
If I click on submit, it saves all of these links for the active user as well. How can I make it pull only the active user’s data?
probably you are not give correct Queryset
in init?
queryset=SocialURL.objects.filter(user_id = activeuser.id))
if request.method == 'POST':
formset = formset_class(data=request.POST, queryset=queryset)
...
else:
formset = formset_class(queryset=queryset)
...
More here: https://docs.djangoproject.com/en/4.0/ref/forms/models/#django.forms.models.modelformset_factory