开发者

Django: ModelForm on save returning object of Another Class

开发者 https://www.devze.com 2023-01-28 22:58 出处:网络
ipdb debug: ipdb> form_class <class \'myproject.apps.usersites.forms.IndividualSiteHomeForm\'>

ipdb debug:

ipdb> form_class
<class 'myproject.apps.usersites.forms.IndividualSiteHomeForm'>
ipdb> form = form_class(request.POST)
ipdb> form
<myproject.apps.usersites.forms.IndividualSiteHomeForm object at 0x021A81F0>
ipdb> var = form.save( commit= False)
ipdb> var
<IndividualProfile: user1>
ipdb> request.POST
<QueryDict: {u'csrfmiddlewaretoken': [u'208ff2a5a78bd5c2ba9452b365b59b6d'], u'ho
me_content': [u'Some contents']}>

I am saving a IndividualSiteHomeForm after binding it to POST data. Why is it returning a IndividualProfile Object?

For reference: 1> Mod开发者_运维问答el

class IndividualSite(SiteBase):
individual = models.ForeignKey(IndividualProfile, unique=True, verbose_name = _("Professional"))
logo = models.ImageField(upload_to="sites/logos/",verbose_name=_("logo"))    
home_content = models.TextField(_("Home contents"), null=True, blank=False,
                                help_text = "This text will appear on your web site home. Do not use HTML here.")    

def __unicode__(self):
    return self.individual.name

2> ModelForm

class IndividualSiteHomeForm(ModelForm):
class Meta:
    model = IndividualSite
    exclude = ('individual','user','logo')

Thanks for your time.

EDIT: To confirm var is indeed IndividualProfile:

ipdb> var
<IndividualProfile: user1>
ipdb> var.home_content
*** AttributeError: 'IndividualProfile' object has no attribute 'home_content'
ipdb> var.__class__
<class 'profiles.models.IndividualProfile'>
ipdb>


I don't think it is returning an instance of IndividualProfile. I think it's returning an instance of IndividualSite, as you expect, but when you repr that from the shell, it uses the __unicode__ method of IndividualSite - which returns the value of the ForeignKey, which is an IndividualProfile.

0

精彩评论

暂无评论...
验证码 换一张
取 消