Saturday, 15 August 2015

python - Setting a Generic Foreign Key in Django while still sane -



python - Setting a Generic Foreign Key in Django while still sane -

i'm banging head against wall trying set generic foreign key. im going post much code perchance can , i'll seek 1 time again in hour.

i've read documentation 1000000 times doesn't seem helping.

here i'm doing in view.

def create_stream(request): stream_form = streamform() comment_form = commentdataform() if request.post: stream_form = streamform(request.post) comment_form = commentdataform(request.post) if stream_form.is_valid() , comment_form.is_valid(): effort = comment_form.save() stream_form.save(commit=false) stream_form.content_object = effort stream_form.save() homecoming httpresponseredirect('/main/') else: httpresponse('nope') context = {'form1':stream_form, 'form2':comment_form} template = 'nregistration.html' homecoming render(request, template, context)

the forms modelforms (for ease of utilize , can utilize save function). this

class streamform(forms.modelform): class meta: model = stream exclude = ['object_id', 'content_object'] class commentdataform(forms.modelform): class meta: model = commentdata

my relevant classes this

class stream(models.model): uid = models.charfield(max_length=20, null=true, blank=true) str_type = models.charfield(max_length=120, default='abc') creator = models.foreignkey(user, related_name="author", null=true, blank=true) parent = models.foreignkey('self', related_name="child_of", null=true, blank=true) create_timestamp = models.datetimefield(auto_now_add=true, auto_now=false) updated = models.datetimefield(auto_now_add=false, auto_now=true) limit = models.q(app_label='picture', model='commentdata') | models.q(app_label='picture', model='repsonsedata') content_type = models.foreignkey(contenttype,verbose_name='content page',limit_choices_to=limit,null=true,blank=true) object_id = models.positiveintegerfield(verbose_name= 'related object',null=true) content_object = genericforeignkey('content_type', 'object_id') def __unicode__(self): homecoming self.uid class meta: unique_together = ('uid',) class commentdata(models.model): uid = models.charfield(max_length=20, null=true, blank=true) contents = models.charfield(max_length=120, default='abc') create_timestamp = models.datetimefield(auto_now_add=true, auto_now=false) class responsedata(models.model): uid = models.charfield(max_length=20, null=true, blank=true) contents = models.charfield(max_length=120, default='abc') create_timestamp = models.datetimefield(auto_now_add=true, auto_now=false)

it seems simple content_type, object_id , content_object don't want play ball. want create instance of comment info form , assign content_object type. end instance of both stream , comment info content_object returns none (as far can tell httpresponse) , content_type , object id both unset.

is there glaringly obvious/what fool mistakes?

if phone call save() commit=false (in form object), homecoming object hasn’t yet been saved database. go on work object form rather object of model.

try this:

stream_instance = stream_form.save(commit=false) stream_instance.content_object = effort stream_instance.save()

python django forms generic-foreign-key

No comments:

Post a Comment