Django: Form save method - django

I have a form, working off different models and using a through (intermediate) model:
class CourseBooking(BaseModel):
'''Intermediary model linking a person on a course with the related booking'''
course = ForeignKey('ScheduledCourse')
customer = ForeignKey('Customer')
booking = ForeignKey('GroupBooking', blank=True, null=True)
The form is using a basic form instead of Model form, with the fields added in manually:
class CourseBookingForm(Form):
course = ModelChoiceField(queryset=ScheduledCourse.objects.all())
title = CharField(
max_length=255,
widget=Select(choices=TITLE_CHOICES),
required=False
)
gender = CharField(
max_length=255,
widget=Select(choices=GENDER_CHOICES),
required=False
)
first_name = CharField(max_length=255)
surname = CharField( max_length=255)
dob = DateField(required=False)
medical = CharField(required=False, widget = forms.Textarea(attrs={'rows': '4'}))
# miscellaneous notes
notes = CharField(required=False, widget = forms.Textarea(attrs={'rows': '4'}))
email = EmailField(required=False)
phone = CharField(required=False)
address = CharField(
max_length=8188,
widget=Textarea(attrs={'rows':'4', 'cols':'50'}),
required=False)
city = CharField(max_length=255, required=False)
county = CharField(
max_length=255, widget=Select(choices=COUNTY_CHOICES))
postcode = CharField(max_length=255, required=False)
country = CharField(
max_length=255,
widget=Select(choices=COUNTRIES), required=False)
I want to create a save method in the forms.py which will save to the database. What I have at the moment (which is wrong) is and gives the error: IntegrityError: null value in column "customer_id" violates not-null constraint
def save(self):
data = self.cleaned_data
if self.cleaned_data['course']:
crs = self.cleaned_data['course']
course_booking = CourseBooking(course=crs)
course_booking.save()
course = CourseBooking.objects.create(course=data['course'])
course.save()
cust = Customer.objects.create(title=data['title'],
gender=data['gender'],
first_name=data['first_name'],
surname=data['surname'],
dob=data['dob'],
notes=data['notes'],
medical=data['medical'],
content_object=cust,
)
cust.save()
address = Address.objects.create(address=data['address'],
city=data['city'],
county=data['county'],
postcode =data['postcode'],
country=data['country'],
content_object=address,
)
address.save()
email = Email.objects.create(email=data['email'],
content_object=email)
email.save()
phone = Phone.objects.create(number=data['phone'],
content_object=phone)
phone.save()

Just call the code for creation of the course object after creating the customer object.
The issue is, the ForeignKey customer in Course model is required, and you have not set that field while creating the object.
You have a couple of other minor issues which I have fixed in the code. .
def save(self):
data = self.cleaned_data
course_booking = None
if self.cleaned_data['course']:
crs = self.cleaned_data['course']
course_booking = CourseBooking(course=crs)
course_booking.save()
cust = Customer.objects.create(title=data['title'],
gender=data['gender'],
first_name=data['first_name'],
surname=data['surname'],
dob=data['dob'],
notes=data['notes'],
medical=data['medical'],
content_object=cust,
)
#cust.save()
course = CourseBooking.objects.create(course=data['course'], customer = cust)
if course_booking:
course.booking = course_booking
#course.save()
address = Address.objects.create(address=data['address'],
city=data['city'],
county=data['county'],
postcode =data['postcode'],
country=data['country'],
content_object=address,
)
#address.save()
email = Email.objects.create(email=data['email'],
content_object=email)
#email.save()
phone = Phone.objects.create(number=data['phone'],
content_object=phone)
#phone.save()
On another note, I would put this object creation logic in the view, rather than the model_form's save method.

Related

Sending data through postman problem in django

I am trying to send data through postman and it takes one field null. So anyone can help me with that. And also I am a beginner to Django.
this is the error
django.db.utils.IntegrityError: null value in column "user_id"
violates not-null constraint DETAIL: Failing row contains (6, MBBS,
Orenburg University, 2019, null).
My models are:
class MyUser(AbstractBaseUser):
gender_type = (('Male','Male'),('Female','Female'))
user_name = (('Doctor','Doctor'),('Patient','Patient'))
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
user_type = models.CharField(choices=user_name, max_length=7)
full_name = models.CharField(max_length=13,null=False)
mobile_number = models.CharField(max_length=13,null=True)
date_of_birth = models.DateField(auto_now_add=True)
gender = models.CharField(choices=gender_type, max_length=10)
blood_group = models.CharField(max_length=25,null=True)
address = models.CharField(max_length=255, null=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
class Educations(models.Model):
user = models.ForeignKey(MyUser, on_delete=models.CASCADE, related_name='edu_user')
doctor_degree = models.CharField(max_length=25)
university = models.CharField(max_length=25)
passing_year = models.IntegerField()
def __str__(self):
return self.doctor_degree
here the user is for doctor and i'm taking it's foreign key in education model
And its viewset is:
class EducationViewSet(viewsets.ViewSet):
def list(self,request):
eduss=Educations.objects.all()
edu_data_list=[]
for edu in eduss:
edu_data_list.append({
"doctor_degree" : edu.doctor_degree,
"university" : edu.university,
"passing_year" : edu.passing.year,
"doctor_name" : edu.user.full_name,
"doctor_mobile_number": edu.user.mobile_number,
"doctor_date_of_birth": edu.user.date_of_birth,
"doctor_blood_group": edu.user.blood_group,
"doctor_email": edu.user.email
})
return Response({"eduss":edu_data_list})
def create(self,request):
doctor_degree = request.data.get('doctor_degree')
university = request.data.get('university')
passing_year = request.data.get('passing_year')
user = request.user
education = Educations()
education.doctor = user
education.doctor_degree = doctor_degree
education.university = university
education.passing_year = passing_year
education.save()
return Response("successfully saved")

How to use model clean method in django and iterate over fields

I asked a question and, as I have read a little, I now can better express what I need:
How to do model level custom field validation in django?
I have this model:
class StudentIelts(Model):
SCORE_CHOICES = [(i/2, i/2) for i in range(0, 19)]
student = OneToOneField(Student, on_delete=CASCADE)
has_ielts = BooleanField(default=False,)
ielts_listening = FloatField(choices=SCORE_CHOICES, null=True, blank=True, )
ielts_reading = FloatField(choices=SCORE_CHOICES, null=True, blank=True, )
ielts_writing = FloatField(choices=SCORE_CHOICES, null=True, blank=True, )
ielts_speaking = FloatField(choices=SCORE_CHOICES, null=True, blank=True, )
and have this model form:
class StudentIeltsForm(ModelForm):
class Meta:
model = StudentIelts
exclude = ('student')
def clean(self):
cleaned_data = super().clean()
has_ielts = cleaned_data.get("has_ielts")
if has_ielts:
msg = "Please enter your score."
for field in self.fields:
if not self.cleaned_data.get(str(field)):
self.add_error(str(field), msg)
else:
for field in self.fields:
self.cleaned_data[str(field)] = None
self.cleaned_data['has_ielts'] = False
return cleaned_data
What I am doing here is that checking if has_ielts is True, then all other fields should be filled. If has_ielts is True and even one field is not filled, I get an error. If has_ielts is False, an object with has_ielts=False and all other fields Null should be saved. I now want to do it on the model level:
class StudentIelts(Model):
SCORE_CHOICES = [(i/2, i/2) for i in range(0, 19)]
student = OneToOneField(Student, on_delete=CASCADE)
has_ielts = BooleanField(default=False,)
ielts_listening = FloatField(choices=SCORE_CHOICES, null=True, blank=True, )
ielts_reading = FloatField(choices=SCORE_CHOICES, null=True, blank=True, )
ielts_writing = FloatField(choices=SCORE_CHOICES, null=True, blank=True, )
ielts_speaking = FloatField(choices=SCORE_CHOICES, null=True, blank=True, )
def clean(self):
# I do not know what to write in here
and have this model form:
class StudentIeltsForm(ModelForm):
class Meta:
model = StudentIelts
exclude = ('student')
In the clean method of my model I want something with this logic(this is psedue code):
def clean(self):
msg = "Please enter your score."
if self.has_ielts:
my_dic = {}
for f in model_fields:
if f is None:
my_dic.update{str(field_name): msg}
raise ValidationError(my_dic)
How can I do this?
How can I get the same result as my modelform but at the model level?
You need to explicitly declare the fields that should be non-empty, otherwise you're cycling through fields that are not related to your clean method, like id and student. Someone might want to add a field later one that's not mandatory and wonder why it raises a validation error.
class StudentIelts(Model):
# fields
non_empty_fields = ['ielts_reading', ...]
def clean(self):
errors = {}
if self.has_ielts:
for field_name in self.non_empty_fields:
if not getattr(self, field_name):
errors[field_name] = msg
if errors:
raise ValidationError(errors)

django forms add blank choice for a IntegerField

I am trying to add a "null" choice to my form choices, since the field is not required.
Now for char fields I could go for a blank and work around that later, but this wont work for an integer field.
My question, how can I add a Null choice for both my CharField and IntegerField
My choices:
LIGHT = 0
MEDIUM = 1
HEAVY = 2
ARMORTYPES = (
(LIGHT, 'Light'),
(MEDIUM, 'Medium'),
(HEAVY, 'Heavy'),
)
my model:
class Item(models.Model):
name = models.CharField(max_length=63, unique=True)
flavor = models.TextField()
price = models.IntegerField()
weight = models.FloatField()
armor_proficiency = models.IntegerField(choices=ARMORTYPES, null=True)
(some more fields)
form:
class ItemForm(forms.ModelForm):
name = forms.CharField(help_text="name of the item")
flavor = forms.CharField(widget = forms.Textarea, help_text="short description")
price = forms.IntegerField(help_text="its market price")
weight = forms.FloatField(help_text="weight of the item")
armor_proficiency = forms.ChoiceField(choices=BLANK_CHOICE + ARMORTYPES, required=False, help_text="Some help text")
any idea how i could fix this?

Cannot assign "u''": "Company.parent" must be a "Company" instance

I am getting this at every attempt.
Cannot assign "u''": "Company.parent" must be a "Company" instance.
I do not know what else to do.
The view code is still half baked, sorry for that.
Am I passing wrong parameters to the form?
I have the following model:
models.py
class Company(AL_Node):
parent = models.ForeignKey('self',
related_name='children_set',
null=True,
db_index=True)
node_order_by = ['id', 'company_name']
id = models.AutoField(primary_key=True)
company_name = models.CharField(max_length=100L, db_column='company_name') # Field name made lowercase.
next_billing_date = models.DateTimeField()
last_billing_date = models.DateTimeField(null=True)
weekly = 'we'
twice_a_month = '2m'
every_two_weeks = '2w'
monthly = 'mo'
billing_period_choices = (
(weekly, 'Weekly'),
(every_two_weeks, 'Every two weeks'),
(twice_a_month, 'Every two weeks'),
(monthly, 'Monthly'),
)
billing_period = models.CharField(max_length=2,
choices=billing_period_choices,
default=weekly)
objects = CompanyManager()
The following forms.py:
class newCompany(ModelForm):
company_name = forms.CharField(label='Company Name',
widget=forms.TextInput(attrs={'class': 'oversize expand input-text'}))
billing_period = forms.ModelChoiceField
next_billing_date = forms.CharField(widget=forms.TextInput(attrs={'class': 'input-text small', 'id': 'datepicker'}))
parent = forms.CharField(widget=forms.HiddenInput(), required=False)
class Meta:
model = Company
fields = ["company_name", "parent", "billing_period", "next_billing_date"]
The following view:
def create_company(request):
userid = User.objects.get(username=request.user).id
my_company_id = CompanyUsers.objects.get(user_id=userid).company_id
my_company_name = Company.objects.get(id=my_company_id).company_name
machines = Title.objects.raw(
'select machines.id, title.name, machines.moneyin, machines.moneyout, moneyin - moneyout as profit, machines.lastmoneyinoutupdate, (select auth_user.username from auth_user where machines.operator = auth_user.id) as operator, (select auth_user.username from auth_user where machines.readers = auth_user.id) as readers from machines, title where machines.title = title.id and machines.company_id =%s',
[my_company_id])
if request.method == 'POST':
form_company = newCompany(request.POST)
if form_company.is_valid():
new_company = form_company.save(commit=False)
new_company.parent = my_company_id
if request.POST.get('select_machine'):
selected_machine = request.POST.getlist('select_machine')
percentage = request.POST.get('percentage')
if not Beneficiary.objects.check_assign_machine(my_company_id, selected_machine, percentage):
target_company_name = new_company.company_name
target_company_id = Company.objects.get(company_name=target_company_name).id
new_company.save()
Machines.objects.assign_machine(target_company_id, selected_machine)
Beneficiary.objects.create_beneficiary(percentage, target_company_name, my_company_id, selected_machine)
else:
invalid_machines = Beneficiary.objects.check_assign_machine(my_company_id, selected_machine, percentage)
return render(request, 'lhmes/createcompany.html',
{'form_company': form_company, 'machines': machines, 'my_company_name': my_company_name, 'invalid_machines' : invalid_machines})
else:
new_company.save()
else:
form_company = newCompany()
return render(request, 'lhmes/createcompany.html',
{'form_company': form_company, 'machines': machines, 'my_company_name': my_company_name})
The error message says you are trying to set a relationship with a string but Django expects the value to be an instance of the Company model. You should assign the foreign key fields with a real model instance instead of only the primary key.
I've spotted a few places in the code where you are assigning a PK:
new_company.parent = my_company_id
Where the model expects it to be an instance:
new_company.parent = Company.objects.get(id=my_company_id)
I really don't remember if this works, but you can try:
new_company.parent_id = int(my_company_id)
This would spare a trip to the database.

django formset initial data not coming

this is my model :
class Member(models.Model):
profile = models.OneToOneField(Profile, editable=False, null=True)
title = models.CharField(max_length=4, choices=TITLE_TYPES, null=True)
name = models.CharField(max_length=100, null=True, verbose_name='Name')
lastname = models.CharField(max_length=100, null=True, verbose_name='LastName')
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, null=True, verbose_name='Gender')
dob = models.DateField('dob')
redressno = models.CharField(max_length=100, null=True, verbose_name='RedressNo')
this is my form
class MemberForm(ModelForm):
dob = forms.DateField(required=False, input_formats=('%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y'))
class Meta:
model = Member
exclude = ('profile',)
this is my view :
members = Member.objects.filter(profile=profiles)
data1 = serializers.serialize( "python", members)
print data1[0]['fields']
memarr=[]
for index, a in enumerate(data1):
memarr.append(a['fields'])
print memarr
MemberFormSet = formset_factory(MemberForm, formset=BaseFormSet)
member_formset = MemberFormSet(initial=memarr)
#here setting intial array of mem
mdata['form-TOTAL_FORMS']=u'2'
mdata['form-INITIAL_FORMS']=u'0'
mdata['form-MAX_NUM_FORMS']=u''
member_formset = MemberFormSet(mdata)
memberform = MemberForm(mdata)
c = {'form': form, 'memberform': memberform, 'member_formset': member_formset}
c.update(csrf(request))
return render_to_response('edit_profile.html', c, RequestContext(request))
m passing the initial data but still the initial data is not being shown in the form?
Here's your problem:
member_formset = MemberFormSet(initial=memarr)
...
member_formset = MemberFormSet(mdata)
You're setting the initial data on one FormSet instance, and then throwing away that instance with the initial data and overwriting with another new FormSet instance.
You can simplify your view to this:
# If you are not using csrf middleware, use this decorator instead of ghetto magic
#csrf_protect
#render_to('edit_profile.html') # handy decorator from django-annoying
def profile_members_edit(request.profile):
members = Member.objects.filter(profile=profiles)
# iterate over the ValuesQuerySet gives a list of dicts for initial data
member_data = list(members.values())
# setting extra=<> and max_num=<> is easier than setting internal form data
MemberFormSet = formset_factory(MemberForm, formset=BaseFormSet, extra=2)
member_formset = MemberFormSet(initial=member_data)
return {'member_formset': member_formset }