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)
newrecord.save(force_insert=True)
except Pad_info.DoesNotExist:
# display error message and blank the pad_id field
pass
return render(request,
'reclamationdb/inspectioncreate_mobile.html', {'form' : form,})
#return HttpResponseRedirect('/index/')
else:
# invalid form
pass
else:
# do something else
form = create_insp_m()
#return render(request,
'reclamationdb/inspectioncreate_mobile.html', {'form' : form,})
return render(request, 'reclamationdb/inspectioncreate_mobile.html',
{'form' : form,})
The form appears in a web page, and displays all the required information,
including the data from the related one record in the foreign key.
But when I save, I get a Type Error, reading "int() argument must be a
string or a number, not 'Pad_info'".
The traceback that's highlighted is the line "checkPad =
Pad_info.objects.get(pk=of_pad)"
And in the list of local variables, the relevant one seems to be:
"of_pad <Pad_info: 41, N. EZ Jr>"
It's an object rather than a string, and I can't seem to filter out just
the number 41, which is the actual content of pad_id, and the value I'm
trying to save in the foreign key field. This is really driving me crazy!
Thanks!
Dave
--
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 [email protected].
To post to this group, send email to [email protected].
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/69a68b66-8433-4998-9f62-4df3f13c7b03%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.