简体   繁体   English

自定义 django 管理对象详细信息页面 url(对象更改 url)

[英]customize django admin object detail page url(object change url)

I am trying to override the default django admin change url.我正在尝试覆盖默认的 django 管理员更改 url。 In my table i have composite primary key .在我的表中,我有复合主键

class ABC(models.Model):
    code = models.ForeignKey('PQR', models.DO_NOTHING, db_column='code', primary_key=True)
    language = models.ForeignKey(Languages, models.DO_NOTHING, db_column='language')
    me_name = models.TextField()
    common_name = models.TextField(blank=True, null=True)
    abbreviation = models.TextField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'abc'
        unique_together = (('code', 'language'), ('language', 'me_name'),)

Now my admin url in django admin for each object is /admin/home/abc/{{code}}/change/.现在我在 django admin 中每个对象的管理 url 是 /admin/home/abc/{{code}}/change/。 But i have repeated codes in objects because primary key is composite of ('code', 'language').但是我在对象中有重复的代码,因为主键是 ('code', 'language') 的组合。 So for objects which have repeated code are throwing所以对于有重复代码的对象抛出

Error
    MultipleObjectsReturned at /admin/home/abc/X00124/change/
    get() returned more than one ABC -- it returned 2!

here X00124 this code associated with more than one object.此处 X00124 此代码与多个对象相关联。

What i want here that override the modeladmins get_urls() method and construct the url /admin/home/abc/{{code}}/{{language}}/change/.我在这里想要的是覆盖 modeladmins get_urls() 方法并构造 url /admin/home/abc/{{code}}/{{language}}/change/。

I tried but no success.我试过但没有成功。 Help will be appreciated.帮助将不胜感激。

I made a virtual key, that represents compound key as a single value, that allows to use Django admin without code changes -我制作了一个虚拟键,将复合键表示为单个值,它允许在不更改代码的情况下使用 Django 管理员 -

https://viewflow.medium.com/the-django-compositeforeignkey-field-get-access-to-a-legacy-database-without-altering-db-tables-74abc9868026 https://viewflow.medium.com/the-django-compositeforeignkey-field-get-access-to-a-legacy-database-without-altering-db-tables-74abc9868026

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

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