Django 1.6:与南方的一对多关系问题

时间:2014-12-15 06:07:01

标签: python django django-models django-views django-south

在我的django模型和视图中建立关系需要一些可怕的帮助。

在任何人深入了解之前,只想说谢谢!

在一个应用程序上工作我有一对多的关系,我有很多产品,一些特定的产品只与一个网站有关。

我遇到的最大问题之一是当我尝试将外键添加到网站模型时出现此错误:

 ? The field 'Product.website' does not have a default specified, yet is NOT NULL.
 ? Since you are adding this field, you MUST specify a default
 ? value to use for existing rows. Would you like to:
 ?  1. Quit now, and add a default to the field in models.py
 ?  2. Specify a one-off value to use for existing columns now 

我试着在这里使用这个解决方案: Django South - Create Not Null ForeignKey

但是无济于事,不知道在第4步之后该怎么做而且我不知所措。

我的product_extend应用程序中的Models.py

产品型号:

class Product(models.Model):
    """
    The product structure for the application, the products we scrap from sites will model this and save directly into the tables.
    """

    product_name = models.CharField(max_length=254, verbose_name=_('Name'), null=True, blank=True)
    product_price = CurrencyField( verbose_name=_('Unit price') )
    product_slug_url = models.URLField(max_length=200,  null=True, blank=True)
    product_category = models.CharField(max_length=254, blank=True, null=True)
    product_img = models.ImageField('Product Image', upload_to='product_images', null=True, blank=True) 
    product_website_url = models.URLField(max_length=200,  null=True, blank=True) 
    product_website_name = models.CharField(max_length=254, blank=True, null=True)

    #For Admin Purposes, to keep track of new and old items in the database by administrative users
    date_added = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name=_('Date added'))
    last_modified = models.DateTimeField(auto_now=True, null=True, blank=True, verbose_name=_('Last modified') )

    #For Admin Purposes, to make sure an item is active by administrative users
    active = models.BooleanField(default=True, verbose_name=_('Active') )

    # Foreign Key
    website = models.ForeignKey(Website, null=True, related_name='website_to_product')

网站模型

class Website(models.Model):
    name = models.CharField(max_length=254, blank=True, null=True, unique=True)
    description = models.TextField(null=True, blank=True)
    website_slug = models.SlugField(verbose_name=_('Website Slug'), unique=True)
    site_logo = models.ImageField('Websites Logo', upload_to='website_logo_images', null=True, blank=True) 

    menswear = models.BooleanField(default=False, verbose_name=_('Menswear'))
    womenswear = models.BooleanField(default=False, verbose_name=_('Womenswear'))


    active = models.BooleanField(default=True, verbose_name=_('Active'))

修改

我缩短了这个问题,以使其更易于理解,并将第二部分拆分为另一个问题:

Django 1.6: Displaying a particular models Objects in another template

1 个答案:

答案 0 :(得分:0)

是否正确地说,当您进行迁移时,产品和网站都已存在于models.py中?如果是这种情况,则解决方法是为此

输入随机值
     ?  2. Specify a one-off value to use for existing columns now  

或者您可能暂时将null设置为true。然后,在创建外键并添加现有行的键值之后。您可以将其重新设置为false。然后按照https://stackoverflow.com/a/22617012/2774853中的那样运行迁移过程:

    Step 6. Run ./manage.py schemamigration <app> --auto

    Step 7. Run ./manage.py migrate <app>

希望它有所帮助。

相关问题