使用“python manage.py inspectdb --database = mydatabasename”从以下mysql数据库创建模型:
CREATE TABLE `authors` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`birthday` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
CREATE TABLE `books` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`titel` varchar(45) DEFAULT NULL,
`year` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
CREATE TABLE `authors_books` (
`author_id` int(11) NOT NULL,
`book_id` int(11) NOT NULL,
KEY `fk_authors_books_2_idx` (`book_id`),
KEY `fk_authors_books_1` (`author_id`),
CONSTRAINT `fk_authors_books_1` FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_authors_books_2` FOREIGN KEY (`book_id`) REFERENCES `books` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `awards` (
`author` int(11) NOT NULL,
`award_name` varchar(45) NOT NULL,
`year` int(11) DEFAULT NULL,
PRIMARY KEY (`author`,`award_name`),
CONSTRAINT `fk_awards_1` FOREIGN KEY (`author`) REFERENCES `authors` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
模型看起来像这样:
class Authors(models.Model):
name = models.CharField(max_length=45, blank=True, null=True)
birthday = models.DateField(blank=True, null=True)
class Meta:
managed = True
db_table = 'authors'
verbose_name_plural = 'Authors'
def __unicode__(self):
return self.name
class AuthorsBooks(models.Model):
author_id = models.OneToOneField('Authors', models.DO_NOTHING, db_column='author_id', primary_key=True)
book_id = models.OneToOneField('Books', models.DO_NOTHING, db_column='book_id', primary_key=True)
class Meta:
managed = True
db_table = 'authors_books'
unique_together = (('author_id', 'book_id'),)
verbose_name_plural = 'Author Books'
class Awards(models.Model):
author = models.OneToOneField('Authors', models.DO_NOTHING, db_column='author', primary_key=True)
award_name = models.CharField(max_length=45)
year = models.IntegerField(blank=True, null=True)
class Meta:
managed = True
db_table = 'awards'
unique_together = (('author', 'award_name'),)
verbose_name_plural = 'Awards'
class Books(models.Model):
titel = models.CharField(max_length=45, blank=True, null=True)
year = models.IntegerField(blank=True, null=True)
class Meta:
managed = True
db_table = 'books'
verbose_name_plural = 'Books'
def __unicode__(self):
return '{} - {}'.format(self.titel, self.year)
在AuthorsBooks类中,我将两个外键更改为OneToOneFields。
我的admin.py看起来像这样:
from django.contrib import admin
from myapp.models import Authors
...
class AwardsInline(admin.TabularInline):
model = Awards
class AuthorsBooksInline(admin.TabularInline):
model = AuthorsBooks
class AuthorsAdmin(admin.ModelAdmin):
list_display = ("name", "birthday" )
inlines = (AwardsInline, AuthorsBooksInline)
class BooksAdmin(admin.ModelAdmin):
list_display = ("id", "titel", "year" )
admin.site.register(Authors, AuthorsAdmin)
admin.site.register(Books, BooksAdmin)
答案 0 :(得分:0)
我让它工作的唯一方法是不使用“复合”或“复合”主键。我已经添加了一个列id作为mysql表奖和authors_books的主键。 然后我改回来了:
class AuthorsBooks(models.Model):
author_id = models.OneToOneField('Authors', models.DO_NOTHING, db_column='author_id', primary_key=True)
book_id = models.OneToOneField('Books', models.DO_NOTHING, db_column='book_id', primary_key=True)
...
要:
class AuthorsBooks(models.Model):
author_id = models.ForeignKey('Authors', models.DO_NOTHING, db_column='author_id')
book_id = models.ForeignKey('Books', models.DO_NOTHING, db_column='book_id')
...
和
class Awards(models.Model):
author = models.OneToOneField('Authors', models.DO_NOTHING, db_column='author', primary_key=True)
award_name = models.CharField(max_length=45)
year = models.IntegerField(blank=True, null=True)
...
到
class Awards(models.Model):
author = models.ForeignKey('Authors2', models.DO_NOTHING, db_column='author')
award_name = models.CharField(max_length=45)
year = models.IntegerField(blank=True, null=True)
...
我找不到ManyToManyField的解决方案,总是同样的问题。 我认为原因是:
目前Django模型仅支持此集合中的单个列, 否认许多设计,其中表的自然主键是 多列。 Django目前无法使用这些模式;他们 必须改为引入冗余的单列密钥(“代理” 键),迫使应用程序任意和其他不必要的 选择在任何给定实例中用于表的键。
https://code.djangoproject.com/wiki/MultipleColumnPrimaryKeys