Django Forms saving request.user in ManyToMany fields - python

I have a simple Group model that users can be added.
class Group(models.Model):
name = models.CharField(max_length=50)
users = models.ManyToManyField(settings.AUTH_USER_MODEL)
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
related_name='admin_on_group')
date_created = models.DateTimeField(auto_now_add=True)
date_modifies = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
I have a basic CreateView for the group. The logged in user who creates the group get saved in the field created_by. I however ALSO want to save the same logged in user in the field users so that he can participate as a normal member of the group. The problem is that the view ends up ONLY saving the logged in user and the other users passed in from the form field users are not saved.
For example, If a user called 'george' creates a group, he should be added in the created_by and users as well. As of now, when I select other users in the form, only george gets saved in both fields.
class GroupCreateView(CreateView):
form_class = GroupForm
template_name = "groups/group_create.html"
def form_valid(self, form):
form = form.save(commit=False)
form.created_by = self.request.user
form.save()
# Apparently you can only add M2M relationships saves after first
# saving
form.users.add(User.objects.get(pk = self.request.user.pk))
return HttpResponseRedirect(reverse('group_list'))
def get_form_kwargs(self):
kwargs = super(GroupCreateView, self).get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
I have a modelForm that has the following outline.
Note: The initial data passed in the self.fields['users'] below also doesn't show. I have also used a custom model that has phone_number as the USERNAME_FIELD. The querysets passed in the self.fields['users'] works.
class UserModelChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj):
return obj.get_full_name()
class GroupForm(forms.ModelForm):
class Meta:
model = Group
fields = ('name', 'users', )
def __init__(self, *args, **kwargs):
# popping the user from kwargs dictionary that has been
# passed in CreateView
user = kwargs.pop('user', None)
self.user = user # setting self.user to be equal to user above
super(GroupForm, self).__init__(*args, **kwargs)
self.fields['users'] = UserModelChoiceField(
queryset=User.objects.exclude(phone_number=str(user)),
initial=User.objects.get(phone_number=str(user))
)

Since you've saved the form with commit=False, you need to call the form's save_m2m() method to save the many-to-many data after you have saved the instance.
def form_valid(self, form):
instance = form.save(commit=False)
instance.created_by = self.request.user
instance.save()
form.save_m2m()
# Apparently you can only add M2M relationships saves after first
# saving
instance.users.add(self.request.user)
return HttpResponseRedirect(reverse('group_list'))
Note that I've changed the line to instance = form.save(commit=False) to make it clearer that save() returns an instance, and so that you still have access to the form.

Related

Django - How to include all form logic in modelForm and not in a Createview, so that form can be unittested without also testing the view?

I have a Campaign Model, and a CampaignCreateForm which is a ModelForm. The Campaign model has a contact_list field of type JSONField. When a user is creating a campaign using the CampaignCreateForm they upload a CSV file which is processed to create the JSON data for the contact_list field.
What is the best way to approach this so that I can test the form separately from the view?
I've built this using a CampaignCreateView which inherits from CreateView, and included the logic to parse the CSV file and create the JSON data in the views form_valid method, but this makes unit-testing the form (and any form field validation) impossible. I want to test the functions included in the forms clean method. With my current approach the view and form must be tested together, and that feels wrong.
How can I create the form such that all the logic for the form (processing the CSV file to create the JSON data and discarding the uploaded file) is handled in the form only?
My current CreateView and ModelForm are below:
View:
class CampaignCreateView(LoginRequiredMixin, CreateView):
model = Campaign
form_class = CampaignCreateForm # required if you want to use a custom model form, requires `model` also
template_name = "writing/campaign_create.html"
def get_success_url(self):
""" If model has get_absolute_url() defined, then success_url or get_success_url isnt neccessary
"""
user = User.objects.get(username=self.kwargs.get("username"))
return reverse("writing:campaigns", kwargs={"username": user.username})
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs.update({"user": self.request.user})
return kwargs
def form_valid(self, form):
""" by default, form_valid redirects to success_url
"""
form.instance.user = self.request.user
form.instance.image_url = f"https://picsum.photos/seed/{randrange(10000)}/500/300"
file = form.cleaned_data["contact_list_file"]
file_content = file.open("r")
json_contact_list = csv_to_json(file_content)
form.instance.contact_list = json_contact_list
contact_list = json.loads(json_contact_list) # as python dict
form.instance.items = len(contact_list)
response = super().form_valid(form)
log_campaign_progress(pk=form.instance.pk, status="t2h-created", stage="campaign")
enqueue_handwriting_generation(campaign_pk=form.instance.pk)
return response
Form:
class CampaignCreateForm(forms.ModelForm):
contact_list_file = forms.FileField(required=True)
def __init__(self, *args, **kwargs):
self.user = kwargs.pop(
"user"
) # To get request.user. Do not use kwargs.pop('user', None) due to potential security hole
super().__init__(*args, **kwargs)
class Meta:
model = Campaign
fields = ("name", "message", "contact_list_file")
def clean(self):
# Cammpaign name is unique for the user
try:
Campaign.objects.get(name=self.cleaned_data["name"], user=self.user)
except Campaign.DoesNotExist:
pass
else:
self.add_error(
"name",
ValidationError(
_("You've already created a campaign with this name"), code="BadCampaignName"
),
)
# if an error is attached to a field then the field is removed from cleaned_data
self = check_message_length(self)
self = check_message_text_is_valid(self)
self = check_file_is_valid(self)
self = check_message_tags_exist_in_contact_list(self)
return self.cleaned_data
Model:
# TimeStampedModel inherits form models.Model
class Campaign(TimeStampedModel):
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
)
order = models.ForeignKey(
Order,
on_delete=models.SET_NULL,
null=True,
)
name = models.CharField(max_length=80)
notes = models.CharField(max_length=2000, null=False, blank=True)
message = models.TextField(
max_length=1800,
null=False,
blank=False,
)
contact_list = models.JSONField(null=True)
items = models.PositiveIntegerField(null=False, blank=False)
purchased = models.BooleanField(default=False, null=False)
def __str__(self):
return self.name
class Meta:
ordering = ["-modified"]
unique_together = ("user", "name")
... more fields

Django Model Form is not saving data to database, but object is created

I'm using a model form and a class based view and I'm trying to save user post model to db. The problem is that UserPost object is created (I've printed it in console and it has all the data and every time when I create new it has new id, so it seems it works) but nothing is saved to database. I'm not sure where the problem might be. Do you have any idea?
views.py
class CompletePost(View):
def get(self, request, *args, **kwargs):
post_form=myForms.UploadPostForm()
return render(request,'shop/create_post.html',{'post_form':post_form})
def post(self, request, *args, **kwargs):
post_form=myForms.UploadPostForm(request.POST)
print(request.user.id)
if post_form.is_valid():
user_post_v=post_form.save()
transaction.commit()
models.py
class UserPost(models.Model):
user_id=models.ForeignKey(Account,on_delete=models.CASCADE)
title=models.CharField(max_length=255)
text=models.TextField(null=True)
category=models.ForeignKey(Category,null=True,on_delete=models.SET_NULL)
is_used=models.BooleanField(default=False)
price=models.IntegerField(default=0)
created_at = models.DateField(auto_now_add=True)
updated_at = models.DateField(auto_now=True)
is_active = models.BooleanField(default=True)
def __unicode__(self):
return self.id
def get_absolute_url(self,**kwargs):
return reverse_lazy('index')
User FormView for views with forms:
from django.views.generic import FormView
class CompletePost(FormView):
template_name = 'your_template.html'
form_class = UploadPostForm
def form_valid():
new_post = form.save(commit=False)
category = Category.objects.get(pk=self.kwargs['pk']) # or just query your category the way you would do it
user = self.request.user
new_post.category = category
new_post.user_id = user
new_post.save()
return redirect('index') # or wherever you wanna redirect after submitting

Assign Foreign Key Value after creating ( Logged In User )

I have a createview using CBV
class StudentCreate(LoginRequiredMixin, CreateView):
login_url = '/signin/'
model = Student
fields = ['first_name', 'last_name' ]
success_url = '/dashboard/'
Respective models.py
class Class_teacher(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
standard = models.IntegerField()
division = models.CharField(max_length=1)
subject = models.CharField(max_length=200)
email = models.CharField(max_length=30)
class Student(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
classteacher = models.ForeignKey('Class_teacher', on_delete=models.SET_NULL,blank=True, null=True )
The webapp has a login feature . When the user ( classteacher ) login they can add students. What I want is classteacher field in Student(model Form ) should be automatically set as user which is the classteacher. ( Classteacher ) and should be saved in the db after creating the student. Classteacher model is updated with respective required fields .
Look here for the various methods of a CreateView that you can override.
In your case, you want to override the form_valid() method, which is called when the new Student will be saved.
from django.shortcuts import get_object_or_404
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.classteacher = get_object_or_404(Class_teacher, email=self.request.user.email)
self.object.save()
return super().form_valid(form)
You need to define your own form_valid().
I assume the Teacher as a one to one Relationship with your User model.
def form_valid(self, form):
student = form.save(commit=False)
#retrieve the current logged_in teacher, of course you have to be sure this view is only accesible for teachers (in dispatch for exemple)
self.object.classteacher = self.request.user.teacher
self.object.save()
return super(StudentCreate, self).form_vaild(form)
#bonus the dispatch
def dispatch(self, request, *args, **kwargs):
#get the loged in user
if request.user.teacher:
return super(StudentCreate, self).dispatch(request, *args, **kwargs)
else:
raise Http404

Anonymous user error

I'm trying to save form data to an anonymous user, however I get the below error when trying to save some form data in a CreateView". I'm not clear what the issue is?
ValueError: Cannot assign "<SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x11126bc18>>": "EUser.user" must be a "User" instance.
Models:
class EUser(models.Model):
online_account = models.BooleanField()
supplier1 = models.OneToOneField(SupplierAccount)
supplier2 = models.OneToOneField(SupplierAccount)
supplier3 = models.OneToOneField(SupplierAccount)
address = models.OneToOneField(Address)
user = models.ForeignKey(settings.AUTH_USER_MODEL)
class SupplierAccount(models.Model):
supplier = models.ForeignKey(Supplier)
username = models.CharField(max_length=255)
password = models.CharField(max_length=255)
Form:
class ServiceTypeForm(forms.ModelForm):
# BOOL_CHOICES = ((False, 'No'), (True, 'Yes'))
# online_account = forms.BooleanField(widget=forms.RadioSelect(choices=BOOL_CHOICES))
def __init__(self, *args, **kwargs):
super(ServiceTypeForm, self).__init__(*args, **kwargs)
self.fields['service_type'].initial = 'D'
class Meta:
model = EUser
fields = ('service_type', 'online_account')
View:
class ServiceTypeView(CreateView):
form_class = ServiceTypeForm
template_name = "standard_form.html"
success_url = '/'
def form_valid(self, form):
form.instance.user = self.request.user
super().form_valid(form)
online_account = form.cleaned_data['online_account']
if online_account:
return redirect('../online')
else:
return redirect('../address')
If the user is not logged in, then request.user is an anonymous user. It doesn't make sense to assign an anonymous user to form.instance.user, because an anonymous user does not exist in the database or have a primary key.
How you change your code depends on how you want your application to work.
If you want to allow anonymous users to create service types, then
# if self.request.user.is_authenticated(): # Django < 1.10
if self.request.user.is_authenticated:
form.instance.user = self.request.user
For this to work, you would need to change the user field to make it optional.
user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True)
After making this change, you'll need to run makemigrations and then migrate, to update the database.
Another option would be to restrict the view to logged in users. In Django 1.9+, You can do this with the LoginRequiredMixin.
from django.contrib.auth.mixins import LoginRequiredMixin
class ServiceTypeView(LoginRequiredMixin, CreateView):
...
I think you can not use the AnonymousUser as value for a ForeignKey to a User.
You should keep is as Null in this case.
class EUser(models.Model):
...
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, default=None)
class ServiceTypeView(CreateView):
...
def form_valid(self, form):
if self.request.user.is_authenticated():
form.instance.user = self.request.user
...

Referencing the current user in Class Based Views (CBV)

I've implemented a form where I require fields in the User object to be populated (firstname, lastname, email) as well as fill out a new membership object. I've implemented this with a Function Based View (FBV) but I feel like I should be able to do this with a Class Based View (CBV). The heart of the problem seems to be referencing the current user in a form without passing in the user object. In FBV it's easy to do but I can't find any examples using CBV. I'm thinking that I must be missing something here.
Here is my code
models.py
class Membership(models.Model):
"""Represents an active membership of a user. Both the start_date and
end_date parameters are inclusive."""
DEFAULT_DURATION = 365 # the default number of days a membership is active
start_date = models.DateField(auto_created=True)
end_date = models.DateField(null=True)
membership_type = models.ForeignKey(MembershipType)
referral = models.ForeignKey(User, related_name='membership_referral', null=True)
# Contact Info
phone = PhoneNumberField()
# Address Fields
address_1 = models.CharField(max_length=255)
address_2 = models.CharField(max_length=255, blank=True)
city = models.CharField(max_length=64)
state = USStateField()
zip_code = USPostalCodeField()
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
"""Overload the save function to set the start and end date."""
self.start_date = datetime.date.today()
self.end_date = (self.start_date +
datetime.timedelta(days=self.membership_type.period))
super().save()
#property
def is_active(self):
return self.end_date >= datetime.date.today()
forms.py
class MembershipForm(ModelForm):
"""The Form shown to users when enrolling or renewing for membership."""
def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user", None)
_fields = ('first_name', 'last_name', 'email',)
_initial = model_to_dict(self.user, _fields) if self.user is not None else {}
super(MembershipForm, self).__init__(initial=_initial, *args, **kwargs)
self.fields.update(fields_for_model(User, _fields))
self.fields['referral'].required = False
class Meta:
model = Membership
fields = ['membership_type', 'referral', 'phone', 'address_1',
'address_2', 'city', 'state']
zip_code = USZipCodeField(max_length=5, required=True)
def save(self, *args, **kwargs):
self.user.first_name = self.cleaned_data['first_name']
self.user.last_name = self.cleaned_data['last_name']
self.user.email = self.cleaned_data['email']
self.user.save()
profile = super(MembershipForm, self).save(*args, **kwargs)
return profile
views.py
#login_required
def enroll(request):
template_name = 'enroll.html'
if request.method == 'POST':
form = MembershipForm(request.POST, user=request.user)
if form.is_valid():
form.save()
return redirect('/')
else:
form = MembershipForm(user=request.user)
return render(request, template_name, {'form': form})
You can access current user in class based view by self.request.user. It can be set in FormView by redefining validate method like this:
class YourView(CreateView)
...
def form_valid(self, form):
form.instance.user = self.request.user
return super(YourView, self).form_valid(form)
I have used CreateView instead of FormView in example because for edit you should check current instance's user in additional for security purposes.
Although your question mentions CBV, yet in the code you are using FBV.
In FBV you have access to request variable being passed. You can use request.user in this case.
In case of CBVs, Django allows you to access request object as self.request. In the implementation of default 'django.views.generic.base.View' class of CBV, they do this as first thing.
Check 4th line of def view as part of as_view in this code - https://github.com/django/django/blob/master/django/views/generic/base.py
All the objects, including user as part of request can be accessed as self.request.user.

Categories