简体   繁体   English

Django admin - 编辑页面上的错误模型对象

[英]Django admin - Wrong model object at edit page

I have 3 inherited classes (Fitters -> Workers -> Staffs) connected with tables in my Database (class names in the plural, but that's not important now). 我有3个继承的类(Fitters - > Workers - > Staffs)与我的数据库中的表连接(类名为复数,但现在不重要)。

User can add/edit/remove only Fitters. 用户只能添加/编辑/删除Fitters。 The Workers and Staffs tables are updated automatically (cascaded). 工人和员工表自动更新(级联)。

It works fine: when I add new Fitter, all changes come to all tables in database. 它工作正常:当我添加新的Fitter时,所有更改都会到达数据库中的所有表。 But when I want to edit any Fitter via Django admin tool, I go to edit Fitter page and I see incorrectly filled fields. 但是当我想通过Django管理工具编辑任何Fitter时,我会去编辑Fitter页面,我看到填充字段不正确。

For example: 例如:

  • In Staffs table I have "John Smith" with id=41 在Staffs表中,我有“John Smith”,id = 41
  • In Workers table I have the record with id=21 and ForeignKey=41 (to John Smith) 在Workers表中,我有id = 21和ForeignKey = 41的记录(对John Smith)
  • In Fitters table I have the record with id=5 and ForeignKey=21 (to record in Workers table) 在Fitters表中,我有id = 5和ForeignKey = 21的记录(记录在Workers表中)

When I go to edit Fitter "John Smith" page, I see all fields filled by "Kevin Thomas" (id=21 in Staffs table!). 当我去编辑Fitter“John Smith”页面时,我看到所有字段都填充了“Kevin Thomas”(员工表中的id = 21!)。 So, Django misses the Workers table and goes directly to the Staffs table. 所以,Django错过了Workers表并直接进入Staffs表。

How can I fix it? 我该如何解决?

Here is my draft code: 这是我的草案代码:

class Staffs(models.Model):
    id = models.AutoField(primary_key=True)

    name = models.CharField(max_length=135, blank=True)
    surname = models.CharField(max_length=135, blank=True)

    def __unicode__(self):
        return self.name + " " + self.surname

    class Meta:
        db_table = u'staffs'

class Workers(Staffs):
    idWorker = models.AutoField(primary_key=True, db_column='id')

    staffs_idstaff = models.OneToOneField('Staffs', db_column='Staffs_idstaff', parent_link=True)
    brigades_idbrigade = models.ForeignKey('Brigades', db_column='Brigades_idBrigade')

    def __unicode__(self):
        return self.staffs_idstaff.name + " " + self.staffs_idstaff.surname

    class Meta:
        db_table = u'workers'

class Fitters(Workers):
    idFitter = models.AutoField(primary_key=True, db_column='id')

    qualification = models.CharField(max_length=135, blank=True)
    workers_idworker = models.OneToOneField('Workers', db_column='Workers_idWorker', parent_link=True)

    def __unicode__(self):
        staff = self.workers_idworker.staffs_idstaff
        return staff.name + " " + staff.surname

    class Meta:
        db_table = u'fitters'

EDIT1: EDIT1:

I tried to change my code like this: 我试着像这样改变我的代码:

class Staffs(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=135, blank=True)
    surname = models.CharField(max_length=135, blank=True)

    class Meta:
        db_table = u'staffs'

class Workers(Staffs):
    idWorker = models.AutoField(primary_key=True)
    brigades_idbrigade = models.ForeignKey('Brigades')

    class Meta:
        db_table = u'workers'

class Fitters(Workers):
    idFitter = models.AutoField(primary_key=True)
    qualification = models.CharField(max_length=135, blank=True)

    class Meta:
        db_table = u'fitters'

It's pretty simple now. 现在很简单。 I synced my database, but I have the absolutely same problem! 我同步了我的数据库,但我有完全相同的问题!

EDIT2: EDIT2:

Part of my admin.py file: 我的admin.py文件的一部分:

from django.contrib import admin
from appclient.models import *

admin.site.register(Fitters)
admin.site.register(Staffs)
admin.site.register(Workers)
...

SOLUTION: 解:

Solution is I don't need use my own id's and ForeignKey's for each model. 解决方案是我不需要为每个模型使用我自己的id和ForeignKey。 Djando automatically creates special field for each model and uses it as an id (PrimaryKey) and for link to the parent tables (ForeignKey). Djando自动为每个模型创建特殊字段,并将其用作id(PrimaryKey)和链接到父表(ForeignKey)。

Here is solution: 这是解决方案:

class Staffs(models.Model):
    name = models.CharField(max_length=135, blank=True)
    surname = models.CharField(max_length=135, blank=True)

    class Meta:
        db_table = u'staffs'

class Workers(Staffs):
    brigades_idbrigade = models.ForeignKey('Brigades')

    class Meta:
        db_table = u'workers'

class Fitters(Workers):
    qualification = models.CharField(max_length=135, blank=True)

    class Meta:
        db_table = u'fitters'

Thanks to everyone who helped me. 感谢所有帮助过我的人。

This does seem somewhat strange. 这看起来有些奇怪。 However, as far as I understand, Django will automatically setup the required one-to-one mappings between parents and children, when using multi-table inheritance . 但是,据我所知,当使用多表继承时,Django将自动设置父和子之间所需的一对一映射。 As you have also set these up manually, it might very well be that some kind of inconsistency is introduced due this. 由于您还手动设置了这些,因此可能会出现某种不一致性。

If you take a look directly in the database, are the two different one-to-one relations consistent? 如果你直接看一下数据库,两个不同的一对一关系是否一致?

What happens if you remove the explicit one-to-one fields from your models? 如果从模型中删除显式的一对一字段会发生什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM