Is it possible to Post Foreign Data in Django Rest Framework rather than the "ID" of the data? I was looking into HyperlinkedModelSerializer, however I felt that there wasn't enough documentation on the DRF website. I have two models. Showtime has a link to Cinema.
class Cinema(models.Model):
name = models.TextField("Name",)
slug = models.SlugField("Slug",blank=True)
phone = models.TextField("Phone")
phone2 = models.TextField("Secondary Phone", blank=True, null=True)
email = models.EmailField("Email")
website=models.TextField("Website")
location = models.TextField("Location")
def __unicode__(self):
name = (self.name).title()
return name
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Cinema, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('Cinema:detail', kwargs = {'slug': self.slug })
class Showtime(models.Model):
cinema = models.ForeignKey(Cinema)
showDate = models.TextField("Date",)
showTime = models.TextField("Time",)
slug = models.SlugField("Slug", blank=True, unique=True)
def save(self, *args, **kwargs):
self.slug = uuid.uuid4()
super(Showtime, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('Showtime:detail', kwargs = {'slug': self.slug})
Each model have been serialized using DRF. Is is possible to post Cinema Data via the Showtime API View rather than have to post cinema in Cinema API View first then get ID of that field then post via the Showtime View to link the data?
Basically, All I want is full writable Foreign Key support, or Does DRF support something like get_or_create() ?
You can nest serializers. See the docs at http://www.django-rest-framework.org/api-guide/serializers#dealing-with-nested-objects for an example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With