简体   繁体   English

使用其他模型的Django模型选择

[英]Django model choices using other models

Schedule is my model that holds all my events in one place (Meetings, vacation, birthdays, etc) but i also want each of these events to be models so that they can be viewed in their own space. Schedule是我的模型,可以将我所有的事件都保存在一个地方(会议,假期,生日等),但我也希望将每个事件都作为模型,以便可以在自己的空间中查看它们。 How would i put the events as choices so that when I add a specific event to my schedule, in this case - a reminder, it adds the reminder to my schedule AND reminders but not to vacation or birthdays. 我将如何选择事件,以便在我将特定事件添加到日程表中时(在这种情况下为提醒)将提醒添加到日程表和提醒中,而不是假期或生日。

This is what i have so far, I believe I would put the individual events into SCHED_CHOICES but i do not know how I would call the models inside of this to make it update and save the event to the specific model and not only the schedule model. 到目前为止,这就是我所拥有的,我相信我会将各个事件放入SCHED_CHOICES中,但是我不知道如何调用其中的模型以使其更新并将事件保存到特定模型中,而不仅仅是时间表模型中。

class Schedule(models.Model):
    SCHED_CHOICES = (

    )
    user = models.ForeignKey(User)
    time = models.DateTimeField()

i also want each of these events to be models so that they can be viewed in their own space. 我也希望每个事件都是模型,以便可以在自己的空间中查看它们。

I would start off with an easier approach. 我将从一个简单的方法开始。 As long as your individual events do not have totally different fields it will make things much easier to handle. 只要您的个别事件没有完全不同的字段,它将使事情变得更容易处理。 You could just use a field event_type on your events table: 您可以在事件表上使用字段event_type

from django.db import models

# Do not call your model Schedule because that's what the whole table
# represents. The django models define only one entity of it.
# So Event would be a suitable name.
class Event(models.Model):
    EVENT_CHOICES = (
        ('meeting', 'Meeting'),
        ('vacation', 'Vacation'),
        ('birthday', 'Birthday'),
        # [...]
    )

    user = models.ForeignKey(User)
    time = models.DateTimeField()
    event_type = models.CharField(max_length=30, choices=EVENT_CHOICES)

To get your schedule you can do this: 要获取您的schedule您可以执行以下操作:

schedule = Event.objects.all()

Now you can see, why Schedule is not a good name for this model. 现在您可以看到,为什么Schedule不是此模型的好名字。 You only have one schedule consisting of all the events from your event model. 您只有一个时间表,其中包含事件模型中的所有事件。

Then in your views you can filter the events eg like this: 然后,您可以在视图中过滤事件,例如:

birthdays_of_greg = Events.objects.filter(user__username='greg', event_type='birthday')

No need for a lot of different models in this simple use case. 在这个简单的用例中,不需要很多不同的模型。 You can create a view that only shows birthdays, vacation etc. or only events for a certain user or a combination of this. 您可以创建仅显示生日,假期等或仅显示特定用户的事件或其组合的视图。

If you need to dynamically create event types you can create an EventType model and replace the event_type CharField with a ForeignKey to that: 如果您需要动态创建事件类型,则可以创建一个EventType模型,并将event_type CharField替换为ForeignKey:

from django.db import models


class Event(models.Model):
    # all your event fields

    event_type = models.ForeignKey('EventType')

class EventType(models.Model):
    name = models.CharField(max_length=50)

If you want to get really advanced you can use generic relations . 如果您想真正进步,可以使用通用关系 This allows you to attach any model to an event with a generic foreign key. 这使您可以使用通用外键将任何模型附加到事件。

I would just add an event_category field to your Schedule model with choices of "reminder", "birthday", etc. Then you can filter by event category: Schedule.objects.filter(event_category__exact='birthday') . 我只是将event_category字段添加到您的Schedule模型中,并选择“ reminder”,“ birthday”等。然后您可以按事件类别进行过滤: Schedule.objects.filter(event_category__exact='birthday')

If you really want a separate model for each event category then you can use a OneToOneField to link the records together: 如果您确实希望为每个事件类别使用单独的模型,则可以使用OneToOneField将记录链接在一起:

class Birthday(models.Model):
    event = models.OneToOneField(Schedule, on_delete='models.CASCADE')
    ...

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

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