同步时模型不会创建表

发布于 2024-09-06 18:52:10 字数 3891 浏览 4 评论 0原文

我有一些 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 函数移到其他地方吗?信号不应该在这里造成问题......

I have some django models for my extended users profile. Problem is that this code does not create tables when syncdb is used (simply nothing happens. No validation errors). Why is that happening? (Also those models give import error elsewhere) :

#!/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'

In contrary this code produces tables flawlessly :

#!/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'

Should I move this user_created function somewhere else ? Signals shouldn't create problems here...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨轻弹 2024-09-13 18:52:10

嗯,你似乎有某种交叉导入;如果您将一些模型导入到 forms 并将某些表单从那里导入回 models 这无法解决,因为在处理 models 需要导入forms,而forms又需要models...这无法解决!

除此之外,我认为不需要在模型模块中导入表单是更好的设计,因为它们与视图更相关!

Well you seem to have some kind of cross import; if you import some models to forms and some form from there back to models this can't be resolve, because when processing models forms is required to be imported, and forms requires models again... This can't be resolved!

Besides this I think it's better design not needing to import forms in a models module, because they are more related to views!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文