我在django中创建了一些模型来代表新闻文章,作者和每篇文章的地理焦点。我需要文章和作者之间的多对多关系,以及文章和位置之间的一对多关系,其中每个位置可以有多个文章,但反之亦然。我尝试了各种方法但每次在Django中运行迁移时都会出现以下错误:
django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "location"
生成表格的代码如下:
from django.contrib.gis.db import models
class Article(models.Model):
article_id = models.AutoField(primary_key=True)
article_title = models.CharField(max_length=200, unique_for_date="pub_date")
pub_date = models.DateTimeField('date published')
article_summary = models.TextField()
title_id = models.CharField(max_length=200)
section_id = models.CharField(max_length=200)
def __unicode__(self):
return u'%s %s' % (self.article_title, self.pub_date)
class Meta:
db_table = 'article'
class Author(models.Model):
author_id = models.AutoField(primary_key=True)
articles = models.ManyToManyField(Article)
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
def __unicode__(self):
return u'%s %s %s' % (self.articles, self.first_name, self.last_name)
class Meta:
db_table = 'author'
class Location(models.Model):
location_id = models.AutoField(primary_key=True)
lat = models.FloatField()
lon = models.FloatField()
local = models.CharField(max_length=200)
city = models.CharField(max_length=200)
region = models.CharField(max_length=200)
country = models.CharField(max_length=200)
continent = models.CharField(max_length=200)
article = models.ForeignKey(Article, default=0)
def __unicode__(self):
return u'%s %s %s' % (self.location_id, self.lat, self.lon)
class Meta:
db_table = 'location'
我认为这是相对简单的事情,但在过去的几天里它已经逃过了我。如果您需要更多信息,请与我们联系。
这是我现在使用的模型代码,但仍然存在上述相同的问题:
from django.contrib.gis.db import models
class Author(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
class Meta:
db_table = 'author'
class Location(models.Model):
lat = models.FloatField()
lon = models.FloatField()
local = models.CharField(max_length=200)
city = models.CharField(max_length=200)
region = models.CharField(max_length=200)
country = models.CharField(max_length=200)
continent = models.CharField(max_length=200)
def __unicode__(self):
return u'%s %s' % (self.lat, self.lon)
class Meta:
db_table = 'location'
class Article(models.Model):
authors = models.ManyToManyField(Author)
location = models.ForeignKey(Location)
article_title = models.CharField(max_length=200, unique_for_date="pub_date")
pub_date = models.DateTimeField('date published')
article_summary = models.TextField()
title_id = models.CharField(max_length=200)
section_id = models.CharField(max_length=200)
def __unicode__(self):
return u'%s %s' % (self.article_title, self.pub_date)
class Meta:
db_table = 'article'
答案 0 :(得分:1)
如果一篇文章可以包含许多作者和一个位置,那么您需要的是与作者和具有位置的外键的多对多关系。
class Article(models.Model):
article_id = models.AutoField(primary_key=True)
authors = models.ManytoManyField(Author)
location = models.ForeignKey(Location)
article_title = models.CharField(max_length=200, unique_for_date="pub_date")
pub_date = models.DateTimeField('date published')
article_summary = models.TextField()
title_id = models.CharField(max_length=200)
section_id = models.CharField(max_length=200)
您不需要位置中的文章ID。
class Location(models.Model):
location_id = models.AutoField(primary_key=True)
lat = models.FloatField()
lon = models.FloatField()
local = models.CharField(max_length=200)
city = models.CharField(max_length=200)
region = models.CharField(max_length=200)
country = models.CharField(max_length=200)
continent = models.CharField(max_length=200)
还值得注意的是,您不必为模型创建一个ID(它们是自动创建的),除非您想为模型ID使用除简单整数之外的其他内容。