简体   繁体   English

Django以Unicode方法反向OneToOneField查找

[英]Django reverse OneToOneField lookup in unicode method

I have schedule and event models like this. 我有这样的时间表和事件模型。

class Schedule(models.Model):
    jan = models.FloatField(default=2.0)
    feb = models.FloatField(default=2.0)

    def __str__(self):
        return 'Some boring value'

class Event(models.Model):
    name = models.CharField(max_length=20)
    schedule = models.OneToOneField(schedule, null=True, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

In my admin view, I want to add the schedule in an EventAdmin class in a collapsed fashion like this. 在我的管理员视图中,我想以这种折叠方式将时间表添加到EventAdmin类中。

class ScheduleAdmin(admin.ModelAdmin):
    fieldsets = [
    ('Schedule', {'fields': ['jan','feb']}),
    ]      

class EventAdmin(admin.ModelAdmin):
    fieldsets = [
    ('Event', {'fields': ['name',]}),
    ('Add schedule', {'fields': ['jan','feb'], 'classes': ['collapse']}),
    ]

So when I add a schedule to an event, I want the schedule str method to return the related Event.name field in the admin form, instead of just 'Some boring value' as it does now. 因此,当我将时间表添加到事件中时,我希望schedule str方法返回管理表单中的相关Event.name字段,而不是像现在那样只是“ Some boring value”。

I also really only want to assign a schedule object once to an Event, instead of the form in admin showing a list of other schedules for other events - I have to disable the add and edit functionality if a schedule is added to an event. 我还真的只想将一个时间表对象分配给一个事件,而不是在admin中的表单中显示其他事件的其他时间表的列表-如果将时间表添加到事件中,则必须禁用添加和编辑功能。

I use admin.TabularInline sometimes, and maybe I should try using this method instead, because it also does not register Schedules in the admin (which I'm not really interested in either) - but this will return the error that 'Schedule has no foreign key to Event' and I have not found a way to solve reverse lookups in the admin yet either. 有时我使用admin.TabularInline,也许我应该尝试使用此方法,因为它也没有在admin中注册Schedules(我对此也不是很感兴趣)-但这将返回错误:“ Schedule没有事件的外键”,而且我也没有找到解决管理员反向查询的方法。

What would be the correct way to proceed? 正确的处理方式是什么?

So when I add a schedule to an event, I want the schedule str method to return the related Event.name field in the admin form, instead of just 'Some boring value' as it does now. 因此,当我将时间表添加到事件中时,我希望schedule str方法返回管理表单中的相关Event.name字段,而不是像现在那样只是“ Some boring value”。

Add related_name='event' to your models.OneToOneField relation, so you can replace return 'Some boring value' with return self.event.name . 在您的models.OneToOneField添加models.OneToOneField related_name='event' models.OneToOneField关系,因此您可以将return self.event.name return 'Some boring value'替换为return self.event.name

As for: 至于:

I also really only want to assign a schedule object once to an Event, instead of the form in admin showing a list of other schedules for other events - I have to disable the add and edit functionality if a schedule is added to an event. 我还真的只想将一个时间表对象分配给一个事件,而不是在admin中的表单中显示其他事件的其他时间表的列表-如果将时间表添加到事件中,则必须禁用添加和编辑功能。

It's not very clear what you want to achieve here. 在这里要实现的目标还不是很清楚。

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

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