Monday, July 28, 2014

Re: Getting the "Real" Foreign Key from a ModelForm

On Mon, Jul 28, 2014 at 2:39 PM, llanitedave
<llanitedave@birdandflower.com> wrote:
>
> This is my first attempt at a work Django Application since the tutorial, so my odds of missing something obvious are pretty high. Still, I've been as far as I can in the documentation, and I can't find anything that addresses this. Using Python 2.7 and Django 1.6.5, in a ModelForm containing a foreign key, rather than returning the key value itself the form returns an object that represents the __unicode__(self) value of the model. So it isn't an integer of a string, and I get a TypeError when I try to save it.
>
> class Pad_info(models.Model):
>
> site_id = models.IntegerField(primary_key=True)
> name = models.CharField('Pad Location', max_length=40, unique=True)
> ...
> (irrelevant fields snipped...)
>
>
>
> def __unicode__(self):
> return u'%s, %s' % (self.site_id, self.name)
>
> def ID(self):
> return self.site_id
>
> class Meta:
> db_table = 'pad_info'
> verbose_name = 'Pad Info'
>
>
> class Inspections(models.Model):
> of_pad = models.ForeignKey(Pad_info)
> insp_date = models.DateField('Inspection Date')
> agency = models.CharField('Agency/ Organization', max_length=40)
> lead_inspector = models.CharField('Lead Inspector', max_length=40)
> accepted = models.BooleanField(default=False)
> comments = models.TextField('Comments/ Notes')
>
> def __unicode__(self):
> return self.agency
>
> def ID(self):
> return self.id
>
> class Meta:
> db_table = 'inspections'
> verbose_name = 'Pad Inspection'
>
>
> Here's my form:
>
> from django import forms
> from django.forms import ModelForm
> from django.forms.fields import DateField
> from reclamationdb.models import Inspections
>
>
> class create_insp_m(ModelForm):
> class Meta:
> model = Inspections
> fields = ['insp_date', 'of_pad', 'agency', 'lead_inspector', 'accepted',
> 'comments']
>
>
> And here's my view, still under construction:
>
> def inspectioncreate_m(request):
> if request.method == 'POST':
> form = create_insp_m(request.POST)
> if form.is_valid():
> # create new inspection record
> insp_date = form.cleaned_data['insp_date']
> of_pad = form.cleaned_data['of_pad']
> agency = form.cleaned_data['agency']
> lead_inspector = form.cleaned_data['lead_inspector']
> accepted = form.cleaned_data['accepted']
> comments = form.cleaned_data['comments']
>
> # find if there is a record that matches pad_id
> try:
> checkPad = Pad_info.objects.get(pk=of_pad)
> newrecord = Inspections(of_pad, insp_date, agency, lead_inspector, accepted,
> comments)

This is where your code is going wrong - you should not create a model
instance by passing positional arguments. The docs instruct you to use
kwargs, and then Django can assign to the correct fields.

Hence "of_pad" is of the correct type, it is just being applied to the
wrong field.

You can use kwargs, but it is much cleaner and less typing to use
Model.objects.create() as you can omit the subsequent save() as
create() creates valid objects (they have pks).

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1J_imqWbX552LPhbdpzjVr-Cr3GPt-w4VJ4Zy%2BS%2Bp_Gdw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment