Django 1.11 管理员 : Create a OneToOne relationship and it's object in the admin

标签 django python-3.x django-models django-admin django-1.11

我有一个简单的应用程序(关于二维码),其中有两个模型。第一个是定义二维码,第二个是给它一个功能。 (对于那些想知道的人:我将它分成两个模型,因为我们的 QR 码很复杂,有时缺乏功能并且是只读的。我希望我们的数据库尽可能规范化)。

这是模型(models.py):

from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.utils.translation import ugettext_lazy as _

from core.behaviors import QRCodeable, UniversallyUniqueIdentifiable
from core.utils import QR_CODE_FUNCTIONS
from model_utils.fields import StatusField
from model_utils.models import SoftDeletableModel, TimeStampedModel

QR_CODE_PREFIX = "QR Code"
QR_CODE_FUNCTION_PREFIX = "Function"
QR_CODE_FUNCTION_MIDFIX = "for"


class QRCode(
    UniversallyUniqueIdentifiable,
    SoftDeletableModel,
    TimeStampedModel,
    models.Model
):
    @property
    def function(self):
        try:
            return self.qrcodefunction.qr_code_function
        except ObjectDoesNotExist:
            return ""

    class Meta:
        verbose_name = _('QR code')
        verbose_name_plural = _('QR codes')

    def __str__(self):
        return f'{QR_CODE_PREFIX} {self.uuid}'


class QRCodeFunction(
    UniversallyUniqueIdentifiable,
    SoftDeletableModel,
    TimeStampedModel,
    QRCodeable,
    models.Model
):
    QR_CODE_FUNCTIONS = QR_CODE_FUNCTIONS
    qr_code_function = StatusField(choices_name="QR_CODE_FUNCTIONS")

    class Meta:
        verbose_name = _('QR code function')
        verbose_name_plural = _('QR code functions')

    def __str__(self):
        return f'{QR_CODE_FUNCTION_PREFIX} {self.qr_code_function} {QR_CODE_FUNCTION_MIDFIX} {self.qr_code}'

mixin QRCodeable 是一个抽象基类,它为函数提供了与二维码的 OneToOne 关系。 mixin UniversallyUniqueIdentifiable 给它一个 uuid。

无论如何,我现在希望能够在 Django 管理中创建带有功能的二维码。所以我写了我自己的管理类(admin.py):
from django.contrib import admin

from .models import QRCode, QRCodeFunction


class QRCodeFunctionInline(admin.TabularInline):
    model = QRCodeFunction
    extra = 0


@admin.register(QRCode)
class QRCodeAdmin(admin.ModelAdmin):
    save_on_top = True
    search_fields = ['qrcodefunction__qr_code_function']
    list_display = (
        '__str__',
        'function',
    )
    inlines = [
        QRCodeFunctionInline,
    ]

此代码导致以下管理界面:

Admin interface of the QR code in the Django admin.

如果我现在点击 add another QR code function ,选择一个功能并点击保存,二维码功能的新实例做不是 被创造!这是为什么?如何编写此模型管理员以便我可以在二维码管理员中为二维码创建功能?

最佳答案

这可能是 Django Admin not saving pre-populated inline fields which are left in their initial state 的副本.您的内联将仅使用默认值,但如果一个或多个字段已更改,默认情况下 Django 的管理员实际上不会创建实例。这是一个痛苦的经历,但 Django 在这里谨慎行事。最好不创建,而不是创建并且必须删除。

根据您的上下文调整的该问题的答案是:

from django.contrib import admin
from django.forms.models import ModelForm

class AlwaysChangedModelForm(ModelForm):
    def has_changed(self):
        """ Should returns True if data differs from initial. 
        By always returning true even unchanged inlines will get validated and saved."""
        return True


class QRCodeFunctionInline(admin.TabularInline):
    model = QRCodeFunction
    form = AlwaysChangedModelForm
    extra = 0

关于Django 1.11 管理员 : Create a OneToOne relationship and it's object in the admin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50793972/

相关文章:

django - 在 generic.DetailView 中访问模型实例

Django REST 框架 : Two different ModelSerializers for the same model

python - Pathlib Path.rename() 创建中间目录的方式?

python - 计数时合并行 - Django/SQL

python - 在 CustomLabelModelChoiceField 中使用 context_processors 变量

django - 如何在 Django REST Framework Mongoengine 中序列化 EmbeddedDocumentListField?

mysql - 如何在 POST 后刷新之前让我的模板 "load"保持一两秒钟

python - 使用 SIGINT 终止 Python 3 中的函数

python - 是否可以强制 Numpy 在进行类型转换时使用 cutout 而不是溢出?

django - 如何为谷歌表单等字段设计数据库?