I have a Sighting model which has a foreign key field "notes" to a
model called Notes.
Notes has a predefined empty note with id=1. If the user specifies
no note then
I want the sighting to reference this predefined empty note.
So in my view:
note = form.cleaned_data['notes']
if note != "": # notes field not blank, save note to db
notes_object = Notes(notes=note)
notes_object.save()
else: #get blank note instance
notes_queryset = Notes.objects.get(
id__exact=1
)
notes_object = notes_queryset[0]
sighting = Sighting(
#more fields here
notes = notes_object,
#more fields
)
I have two questions about this.
First, sometimes I'm getting an error:
TypeError: 'Notes' object is unindexable (on the line with
notes_queryset[0] )
Am I doing this right? The Notes with id=1 is definitely present in
the database.
I thought a queryset always returned a list of model instance objects
(in this case a list of 1).
Second, when the note is empty, is there a way to avoid querying the
database to create the instance of Notes? Since I already know the id
and all the contents, the query gives no new information. Just
curious, not a big performance hit!
Thanks
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---