内联管理,没有ForeignKey关系 [英] Admin inline with no ForeignKey relation

查看:39
本文介绍了内联管理,没有ForeignKey关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以手动指定一组相关对象以在没有外键关系的情况下以内联方式显示?

 #父级类Diary(models.Model):日= models.DateField()活动= models.TextField()# 孩子Sleep(models.Model)类:start_time = models.DateTimeField()end_time = models.DateTimeField()SleepInline(admin.TabularInline)类:型号=睡眠def get_queryset(self,request):#返回所有start_time和end_time在Diary.day内的Sleep对象返回Sleep.objects.filter(XXX)类DiaryAdmin(admin.ModelAdmin):内联=(SleepInline,) 

我希望我的 Diary 模型管理员为 start_time 等同于 Diary的同一天的 Sleep 模型显示内联.day .问题是 Sleep 模型没有 Diary ForeignKey (相反,该关系通过使用日期而隐含)./p>

使用上述内容,Django立即抱怨

 <类'records.admin.SleepInline'> ;:(admin.E202)'records.Sleep'没有对'records.Diary'的ForeignKey. 

如何在 Diary 管理页面上以内联方式显示相关的 Sleep 实例?

解决方案

首先让我向您展示您的逻辑缺点:

  • 添加外键时,有2种不常见的操作需要调整关系:创建新的睡眠对象并更新睡眠对象上的时间.
  • 当不使用外键时,每次请求日记时,都需要查找对应的Sleep对象.我假设阅读日记比改变睡眠对象更为普遍,因为在大多数项目中都会如此.

您注意到的另一个缺点是,您不能使用关系功能.InlineAdmin是一个关系功能,因此,正如您所说的使管理工作正常进行"一样,实际上您需要用锤子来松开螺栓.

但是...管理员使用了ModelForm.因此,如果您使用表单集(不能是内联表单集(出于相同原因)并自己处理保存该表单集,这应该是可能的.InlineFormset和InlineAdmin的全部目的是使从相关模型生成表单集更加容易,并且它需要知道这种关系.

最后,您可以添加网址并构建自定义页面,然后在扩展admin/base.html模板时,您将有权访问布局和javascript组件.

Is it possible to manually specify the set of related object to show in an inline, where no foreign key relation exists?

# Parent
class Diary(models.Model):
    day = models.DateField()
    activities = models.TextField()

# Child
class Sleep(models.Model):
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()

class SleepInline(admin.TabularInline):
    model=Sleep
    def get_queryset(self, request):
        # Return all Sleep objects where start_time and end_time are within Diary.day
        return Sleep.objects.filter(XXX) 

class DiaryAdmin(admin.ModelAdmin):
    inlines = (SleepInline, )

I want my Diary model admin to display an inline for Sleep models that have start_time equal to the same day as Diary.day. The problem is that the Sleep model does not have a ForeignKey to Diary (instead, the relation is implicit by the use of dates).

Using the above, Django immediately complains that

<class 'records.admin.SleepInline'>: (admin.E202) 'records.Sleep' has no ForeignKey to 'records.Diary'.

How can I show the relevant Sleep instances as inlines on the Diary admin page?

解决方案

Let me start by showing you the drawbacks of your logic:

  • When adding a foreign key, there are 2 operations, that are uncommon that require adjusting the relation: creating a new sleep object and updating the times on the sleep object.
  • When not using a foreign key, each time a diary is requested the lookup for the corresponding Sleep object(s) needs to be done. I'm assuming reading diaries is much more common then alterations of sleep objects, as it will be in most projects out there.

The additional drawback as you've noticed, is that you cannot use relational features. InlineAdmin is a relational feature, so as much as you say "making the admin work", it is really that you demand a hammer to unscrew a bolt.

But...the admin makes use of ModelForm. So if you construct the form with a formset (which cannot be a an inline formset for the same reason) and handle saving that formset yourself, it should be possible. The whole point of InlineFormset and InlineAdmin is to make generation of formsets from related models easier and for that it needs to know the relation.

And finally, you can add urls and build a custom page, and when extending the admin/base.html template, you will have access to the layout and javascript components.

这篇关于内联管理,没有ForeignKey关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆