gpt4 book ai didi

Django 在管理界面中订购了 ManyToManyField

转载 作者:行者123 更新时间:2023-12-01 12:00:49 25 4
gpt4 key购买 nike

我有一个遗留数据库,其中包含文档和作者的表格。第三个表定义了文档和作者之间的有序多对多关系,使用文档和作者的外键以及一个整数来指定给定文档的作者顺序。

使用 Django 1.1.1(或 SVN),有没有办法在管理页面中编辑文档作者及其顺序?

最佳答案

这很快而且有点粗糙,但它应该让你接近 (r)。

from django.db import models
from django.contrib import admin

class Document(models.Model):
name = models.CharField(max_length = 128)

def __unicode__(self):
return self.name

class Author(models.Model):
name = models.CharField(max_length = 128)
document = models.ManyToManyField(Document, through = 'Foo')

def __unicode__(self):
return self.name

class Foo(models.Model):
document = models.ForeignKey(Document)
author = models.ForeignKey(Author)
author_order = models.IntegerField()

class FooInline(admin.TabularInline):
model = Foo

class DocumentAdmin(admin.ModelAdmin):
inlines = [ FooInline ]

admin.site.register(Author)
admin.site.register(Document, DocumentAdmin)

http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany http://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects

关于Django 在管理界面中订购了 ManyToManyField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1743005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com