简体   繁体   English

要求ForeignKey对象是另一个模型的相关对象

[英]Require ForeignKey object to be a related object of another model

I have a model 我有一个模特

class EventArticle(models.Model):
    event = models.ForeignKey(Event, related_name='article_event_commentary')
    author = models.ForeignKey(Person)

and another model 和另一个模型

class Event(models.Model):
    attendies = models.ManyToManyField(Person, related_name="people")

How do I restrict an author to only objects that are also attendies ? 如何限制一个author只说也是对象attendies

Typically, the ForeignKey limit_choices_to argument is your friend in situations like this. 通常,在这种情况下, ForeignKey limit_choices_to参数是您的朋友。 (See https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to ) (请参阅https://docs.djangoproject.com/zh-CN/1.8/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to

You could restrict the author to a list of users of have attended any event. 您可以将作者限制为已参加任何活动的用户列表。 However, even doing that is far from ideal: 但是,即使这样做也不是理想的选择:

# don't try this at home, this will be excruciatingly slow...
def author_options():
    all_attendee_ids = []
    for e in Event.objects.all():
        for a in e.attendies.all():
            all_attendee_ids.append(a.id)
    return {'author': Person.objects.filter(id_in=set(all_attendee_ids)).id}

# requires Django 1.7+
class EventArticle(models.Model):
    event = models.ForeignKey(Event, related_name='article_event_commentary')
    author = models.ForeignKey(Person, limit_choices_to=author_options)

However, even though you didn't explicitly state it in your question, I suspect you want authors to be limited to a set of attendees from that particular event, ie the same event as specified in the EventArticle model's event field. 但是,即使您没有在问题中明确声明它,我也怀疑您希望作者限于该特定事件(即EventArticle模型的event字段中指定的同一事件)的一组参与者。

In which case, this brings about two problems which I don't believe can be solved cleanly: 在这种情况下,这带来了两个我认为无法彻底解决的问题:

  1. you can't pass parameters (ie the event ID) when using limit_choices_to, and 使用limit_choices_to时,您不能传递参数(即事件ID),并且
  2. until the EventArticle model has a value for event, you wouldn't know which event was in question. 直到EventArticle模型具有事件值,您才知道哪个事件有问题。

As using limit_choices_to isn't going to work here, there probably isn't a way to fix this cleanly. 由于在这里无法使用limit_choices_to,因此可能没有办法彻底解决此问题。 You could add a method to your EventArticle model which will give you a list of potential authors like so... 您可以在EventArticle模型中添加一个方法,该方法将为您提供潜在的作者列表,例如...

class EventArticle(models.Model):
    event = models.ForeignKey(Event, related_name='article_event_commentary')
    author = models.ForeignKey(Person)

    def author_options(self):
        if self.event:
            return self.event.attendies.all()
        else:
            return []

...but you will still need to do some legwork to make those options available to the UI so the user can select them. ...但是您仍然需要做一些工作以使这些选项可用于UI,以便用户可以选择它们。 Without knowing more about your setup, I'd be guessing if I tried to answer that part. 在不了解您的设置的情况下,我会猜测是否尝试回答该部分。

您可以重写EventArticle模型的save()来确保它。

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

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