2010-11-22 100 views
3

我正在嘗試爲用戶創建一個小應用程序來獲取聯繫人。我使用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)) 
+0

爲什麼你有一個FK的配置文件,而不是用戶本身? – 2010-11-22 20:01:15

回答

9

基本上Django的配置文件是爲了別的東西 - 它只是幫助在應用程序中創建和管理用戶配置文件。

首先 - 您應該通過ForeignKey將Contact模型直接鏈接到django.contrib.auth.models.User。通過這種方式,您可以通過簡單的查詢即訪問給定用戶的聯繫人。 User.contact_set.all() - 它會返回給您一個用戶的聯繫人列表。這也將擺脫你的錯誤。

first_name,last_name這樣的第二檔已經在django.contrib.auth.models.User中定義了,所以不需要在UserProfile中再次定義它們。閱讀用戶模型這裏來源:http://goo.gl/8oDv3

三 - 如果你的用戶只能有一個配置文件,你不打算使用Django的很老的版本,那麼你應該使用OneToOneField代替ForeignKey

四件事 - 你很可能使用與Django的捆綁在一起的一個通用視圖省略RequestContext()用法 - 讀到這裏:http://goo.gl/U4DWs

最後一件事 - 記住,用於處理用戶主要模式是User模型本身。任何自定義配置文件只是一個擴展,因此將與用戶相關的所有內容鏈接到用戶模型本身。

快樂編碼!

+0

與第3點相關:http://stackoverflow.com/questions/3221745/django-when-extending-user-better-to-use-onetoonefielduser-or-foreignkeyuser – 2010-11-22 20:31:20

1

爲了詳細說明bx2's answer,你跟模型應該看起來更像是這樣的:

class Contact(models.Model):  
    user = models.ForeignKey(User, related_name='contacts') 
    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) 

你的觀點,坦率地說,並沒有太大的意義。一般來說,內聯表單旨在表示與某些父記錄相關的記錄。在你的情況下,這將是用戶記錄,聯繫人是「兒童」記錄。但同時具有聯繫人窗體和AddContactFormset沒有多大意義。

此外,我建議不要在Django中使用變量名稱,如ffs。除了索引,計數器等變量之外,使用短1-2個字符的變量名稱不起任何作用。繼續使用較長的變量名稱,如formformset,或者甚至使用contact_formcontact_formset。它使得將來更容易調試代碼。

相關問題