私はかなりdjangoの新人です。私はdjango.contrib.auth.modelでDjangoの構築したUserモデルの拡張子である子テーブルに問題があります。子テーブルはUserProfileであり、Userから継承します。 (私のモデルについては下記を参照してください)DjangoのOneToOneに関連するオートポピュレートの子テーブル
管理サイト以外では、これらのテーブルにデータを入れる方法は実際には分かりません。
私のユーザーオブジェクトを取得し、それにu = User.objects.get(username = 'jane_smith'
という変数を割り当て、次にup = u.userprofile
というコマンドで子テーブルのデータにアクセスします。しかし、私は次のスタックトレースに
>>> 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'")
を得た
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()
に続いて、私は、手動で端子を介してデータを入力することで問題を回避取得実験DoesNotExist: UserProfile matching query does not exist.
:私は次のエラーで報われています
誰でも手伝いますか? 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
あなたのタイトルはOneToOne関係に言及し、実際にはこれは、継承です。それを使わないで、標準OneToOneを使用してください。 –
@ダニエル:理由を理解するのを助けてくれますか?私はそれが早いのが好きでしたが、問題を解決するようなことはありませんでした。 – givehook
あなたが表示する理由と同じです。標準的な関係がある場合は、ユーザーとは別にプロファイル側を作成できます。継承によって、あなたは(痛いハッキングなしで)することはできません。 OneToOnesに問題がある場合は、別の質問をしてください。 –