我正在嘗試爲用戶創建一個小應用程序來獲取聯繫人。我使用django-profiles作爲我的配置文件的基礎。現在一切正常,直到我嘗試提交編輯聯繫表單並收到此錯誤。Django - 將我的模型鏈接到配置文件(UserProfile)模型
Cannot assign "<Contact: Contact object>": "Contact.user" must be a "UserProfile" instance.
對於Django來說,我還不是很熟悉,我甚至不確定是否在這裏採取正確的方法。我的最終目標是讓用戶能夠根據需要添加儘可能多的聯繫人。 任何意見表示讚賞。
延伸用戶我的用戶配置模式是這樣的:
class UserProfile(models.Model):
#User's Info
user = models.ForeignKey(User, unique=True)
first_name = models.CharField("first name", max_length=30)
last_name = models.CharField("last name", max_length=30)
home_address = models.CharField(max_length=50)
primary_phone = PhoneNumberField()
city = models.CharField(max_length=50)
state = USStateField()
zipcode = models.CharField(max_length=5)
birth_date = models.DateField()
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True)
和我接觸模型是這樣的:
class Contact(models.Model):
user = models.ForeignKey(UserProfile)
contact_description = models.CharField("Relation or Description of Contact", max_length=50, blank=True)
contact_first_name = models.CharField("contact first name", max_length=30)
contact_last_name = models.CharField("contact last name", max_length=30)
contact_primary_phone = PhoneNumberField("contact primary phone number")
contact_secondary_phone = PhoneNumberField("contact secondary phone number",blank=True)
和我的觀點:
def editContact(request, username, object_id=False, template_name='contacts/edit.html'):
user = UserProfile.user
AddContactFormset = inlineformset_factory(UserProfile,Contact, extra=1)
if object_id:
contact=Contact.objects.get(pk=object_id)
else:
contact=Contact()
if request.method == 'POST':
f= ContactForm(request.POST, request.FILES, instance=contact)
fs = AddContactFormset(request.POST, instance=contact)
if fs.is_valid() and f.is_valid():
f.save()
fs.save()
return HttpResponse('success')
else:
f = ContactForm(instance=contact)
fs = AddContactFormset(instance=contact)
return render_to_response(template_name ,{'fs':fs,'f':f,'contact': contact}, context_instance=RequestContext(request))
爲什麼你有一個FK的配置文件,而不是用戶本身? – 2010-11-22 20:01:15