post - Django form not updating existing record even when instance is passed -
i have form used create , edit model instance. when editing model instance, form still tries create new record , fails because unique fields exist. passing instance when initializing form.
views.py
def organization_course_detail(request, org_id): ''' courses associated organization ''' template_name = 'users/organization-course-list.html' organization = models.organization.objects.get(id=org_id) if request.method == 'post': print organization.id form = forms.createorganizationform(request.post, instance=organization) if form.is_valid(): print 'form valid' org = form.save(commit=false) org.save() return httpresponseredirect( reverse('users:org-course-list', kwargs={'org_id': org_id})) else: form = forms.createorganizationform(instance=organization)
forms.py
class createorganizationform(forms.modelform): ''' form used create new organization. @ same time, create new course clone of "chalk talk sat" , associate course organization , student signs organization ''' class meta: model = models.organization fields = ['name', 'country', 'acronym',]
models.py
class organization(models.model): ''' model have every institution (test prep centers, governments, schools) workshops @ or create school accounts ''' name = models.charfield(max_length=2000) country = countryfield(null=true, blank='(select country)') date = models.datetimefield(default=timezone.now) acronym = models.charfield(max_length=7, help_text="(up 7 characters)") expiration_date = models.datetimefield(default=timezone.now) def get_org_admins(self): return self.admin_for_organizations.all() class meta: unique_together = ( ('name', 'country') ) def __str__(self): return self.name
have looked @ entries in db table? (or can post them?)
you might try using django shell , try execute code hand (query db org_id , check result. edit field , save it. work?)
also think blank
can true or false here.
country = countryfield(null=true, blank='(select country)')
https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.field.blank
Comments
Post a Comment