2011-07-15 67 views
0

我對django相當陌生,而且我在Django的構建User模型django.contrib.auth.model的擴展子表中遇到問題。子表是UserProfile,並從User繼承。 (請參閱下面的我的型號)在Django中OneToOne相關的自動填充子表

我無法真正弄清楚如何將數據放在這些表中,除非管理網站顯然不夠好。

我會得到我的用戶對象併爲它指定一些變量,比如說,u = User.objects.get(username = 'jane_smith'然後嘗試使用up = u.userprofile命令到達子表中的數據。我獎勵了以下錯誤:DoesNotExist: UserProfile matching query does not exist.

我嘗試通過終端獲得解決該問題通過手動輸入數據,與up = UserProfile(user_ptr = User.objects.get(username = 'jane_smith'), country = 'USA', num_meals_hosted = 0, cook_rating = 5, image = None, zipcode =91011)其次up.save()

但是,我得到了下面的堆棧跟蹤

>>> up.save() 
Traceback (most recent call last): 
File "<console>", line 1, in <module> 
File "/Library/Python/2.6/site-packages/django/db/models/base.py", line 460, in save 
self.save_base(using=using, force_insert=force_insert, force_update=force_update) 
File "/Library/Python/2.6/site-packages/django/db/models/base.py", line 504, in save_base 
self.save_base(cls=parent, origin=org, using=using) 
File "/Library/Python/2.6/site-packages/django/db/models/base.py", line 526, in save_base 
rows = manager.using(using).filter(pk=pk_val)._update(values) 
File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 491, in _update 
return query.get_compiler(self.db).execute_sql(None) 
File "/Library/Python/2.6/site-packages/django/db/models/sql/compiler.py", line 869, in execute_sql 
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type) 
File "/Library/Python/2.6/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql 
cursor.execute(sql, params) 
File "/Library/Python/2.6/site-packages/django/db/backends/util.py", line 34, in execute 
return self.cursor.execute(sql, params) 
File "/Library/Python/2.6/site-packages/django/db/backends/mysql/base.py", line 86, in execute 
return self.cursor.execute(query, args) 
File "build/bdist.macosx-10.6-universal/egg/MySQLdb/cursors.py", line 174, in execute 
self.errorhandler(self, exc, value) 
File "build/bdist.macosx-10.6-universal/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler 
raise errorclass, errorvalue 
IntegrityError: (1062, "Duplicate entry '' for key 'username'") 

任何幫助?只想找到一種通過API將數據放入這些表中的方法。以下是我的模特。如果需要,歡迎在評論中發佈更多信息。

from django.db import models 
from django.contrib.auth.models import User 

class UserProfile(User): 
    address = models.CharField(max_length=100, blank=True) 
    city = models.CharField(max_length = 50, blank=True) 
    state = models.CharField(max_length = 15, blank=True) 
    zipcode = models.IntegerField(blank=True) 
    country = models.CharField(max_length = 50, blank=True) 
    num_meals_hosted = models.IntegerField() 
    cook_rating = models.IntegerField() 
    diet_restrict = models.CharField(max_length = 600, blank=True) 
    blurb = models.CharField(max_length = 10000, blank=True) 
    website = models.URLField(blank=True) 
    image_height = models.IntegerField(blank = True) 
    image_width = models.IntegerField(blank = True) 

    def __unicode__(self): 
     return self.id 
+0

你的標題中提到OneToOne關係,但實際上這是繼承。不要使用它,請使用標準的OneToOne。 –

+0

@丹尼爾:你能幫我理解爲什麼嗎?我早些時候曾這樣認爲,但似乎也沒有解決問題。 – givehook

+0

正是你展現的原因。如果您有標準關係,則可以與用戶分開創建配置文件一側。繼承,你不能(沒有一些痛苦的黑客攻擊)。如果您遇到OneToOnes問題,請另外提問。 –

回答