同时发布几个tastypie资源

时间:2013-04-18 16:41:21

标签: django django-models tastypie

这是我正在尝试做的事情:

用户在我的应用程序中创建一个事件。这是模型:

class Event(models.Model):
    name = models.CharField(max_length=40)
    organizer = models.ForeignKey(UserProfile)
    description = models.TextField(null=True)
    place = models.TextField(null=True)
    confirmed = models.BigIntegerField(null=True)
    organizer_part = models.BooleanField(default=True)
    slug = models.SlugField()

在此之后,它会发布邀请参加此活动的不同人员以及用户选择的不同日期。以下是模型:

class EventDate(models.Model):
    """Correspondances date-event"""
    event = models.ForeignKey(Event)
    date = models.BigIntegerField()


class EventPeople(models.Model):
    """Correspondances personne-event"""
    event = models.ForeignKey(Event)
    phone_number = models.PositiveIntegerField()
    name = models.CharField(max_length=32)
    answer = models.BooleanField()
    participation = models.NullBooleanField()

我想在一个请求中填写这三个模型。到目前为止,我必须提出三个请求。我看不出我怎么可能这样做。

任何想法都会受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

适用于您的模型的资源是:

class EventResource(ModelResource):
    event_dates = fields.ToManyField(EventDateResource, 'event_dates')
    event_peoples = field.ToManyField(EventPeopleResource, 'event_peoples')
    class Meta:
          queryset = Event.objects.all()

此外,您还必须创建简单的EventDateResource和EventPeopleResource。

在yout模型中再进行一次更改,您需要添加related_names:

class EventDate(models.Model):
    """Correspondances date-event"""
    event = models.ForeignKey(Event, related_name='event_dates')
    date = models.BigIntegerField()


class EventPeople(models.Model):
    """Correspondances personne-event"""
    event = models.ForeignKey(Event, related_name='event_peoples')
    phone_number = models.PositiveIntegerField()
    name = models.CharField(max_length=32)
    answer = models.BooleanField()
    participation = models.NullBooleanField()