模型在同步时不创建表
Posted
技术标签:
【中文标题】模型在同步时不创建表【英文标题】:Models does not create tables when synched 【发布时间】:2011-03-08 15:11:13 【问题描述】:我的扩展用户配置文件有一些 django 模型。问题是,当使用 syncdb 时,此代码不会创建表(根本没有任何反应。没有验证错误)。为什么会这样? (这些模型也会在其他地方出现导入错误):
#!/usr/bin/env python
# encoding: utf-8
from django.db import models
from django.contrib.auth.models import User
from registration.signals import user_registered
from forms import ExtendedRegistrationForm
import hashlib
class InheritedProfile(models.Model):
first_name = models.CharField("Name", max_length=50, blank=True, null=True)
last_name = models.CharField("Last name", max_length=50, blank=True, null=True)
pid = models.CharField("PESEL", max_length=11, blank=True, null=True)
street = models.CharField("Street", max_length=50, blank=True, null=True)
number = models.CharField("Flat/house number", max_length=10, blank=True, null=True)
code = models.CharField("Zip ", max_length=6, blank=True, null=True)
city = models.CharField("City", max_length=50, blank=True, null=True)
class Meta:
abstract=True
class UserProfile(InheritedProfile):
def upload_path(self, field_attname):
filename = hashlib.md5(field_attname).hexdigest()[:4] + "_" + field_attname
return "uploads/users/%s" % (filename,)
user = models.ForeignKey(User, unique=True, related_name='profile')
image = models.ImageField(upload_to=upload_path, verbose_name="Image", blank=True, null=True)
class Meta:
ordering = ['-id']
db_table = 'userprofile'
def __unicode__(self):
return u"%s " % self.user.username
def user_created(sender, user, request, **kwargs):
form = ExtendedRegistrationForm(request.POST)
extended_user = UserProfile(user=user)
extended_user.is_active = False
extended_user.first_name = form.cleaned_data['first_name']
extended_user.last_name = form.cleaned_data['last_name']
extended_user.pid = form.cleaned_data['pid']
extended_user.image = form.cleaned_data['image']
extended_user.street = form.cleaned_data['street']
extended_user.number = form.cleaned_data['number']
extended_user.code = form.cleaned_data['code']
extended_user.city = form.cleaned_data['city']
extended_user.save()
user_registered.connect(user_created)
class Friend(InheritedProfile):
friend_of = models.ForeignKey(UserProfile, related_name='friend_of')
class Meta:
db_table = 'friend'
相反,这段代码完美地生成了表格:
#!/usr/bin/env python
# encoding: utf-8
from django.db import models
from django.contrib.auth.models import User
import hashlib
class InheritedProfile(models.Model):
first_name = models.CharField("Name", max_length=50, blank=True, null=True)
last_name = models.CharField("Last name", max_length=50, blank=True, null=True)
pid = models.CharField("PESEL", max_length=11, blank=True, null=True)
street = models.CharField("Street", max_length=50, blank=True, null=True)
number = models.CharField("Flat/house number", max_length=10, blank=True, null=True)
code = models.CharField("Zip ", max_length=6, blank=True, null=True)
city = models.CharField("City", max_length=50, blank=True, null=True)
class Meta:
abstract=True
class UserProfile(InheritedProfile):
def upload_path(self, field_attname):
filename = hashlib.md5(field_attname).hexdigest()[:4] + "_" + field_attname
return "uploads/users/%s" % (filename,)
user = models.ForeignKey(User, unique=True, related_name='profile')
image = models.ImageField(upload_to=upload_path, verbose_name="Image", blank=True, null=True)
class Meta:
ordering = ['-id']
db_table = 'userprofile'
def __unicode__(self):
return u"%s " % self.user.username
class Friend(InheritedProfile):
friend_of = models.ForeignKey(UserProfile, related_name='friend_of')
class Meta:
db_table = 'friend'
我应该把这个 user_created 函数移到别的地方吗?信号不应在这里造成问题...
【问题讨论】:
是否有可能在forms
中导入models
或特定模型?
我正在导入特定模型。好吧,不知道为什么这个函数与为模型创建表相冲突,但我已经把它移到了另一个文件中。现在一切正常。
【参考方案1】:
好吧,您似乎有某种交叉导入;如果您将一些模型导入到forms
并从那里导入一些表单返回到models
这无法解决,因为在处理models
时需要导入forms
,而forms
需要再次models
...这无法解决!
除此之外,我认为不需要在模型模块中导入表单是更好的设计,因为它们与视图更相关!
【讨论】:
循环导入解决得很好。可能不是模块内的属性查找。 ***.com/questions/3082015/…以上是关于模型在同步时不创建表的主要内容,如果未能解决你的问题,请参考以下文章