class Film models Model title models CharField max_length 50 slug mode

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Film(models.Model):
title = models.CharField(max_length=50)
slug = models.SlugField(max_length=50, blank=True)
released_date = models.DateField()
genre = models.ManyToManyField(Genres)
director = models.ForeignKey(Directors)
actors = models.ManyToManyField(Actors)
actors_as_text = models.CharField(max_length=250)
description = models.TextField()
def get_actors_names(self):
if not self.actors_as_text:
self.actors_as_text = " ".join([actor.name for actor in self.actors.all()])
self.save()
return self.actors_as_text
class Meta:
verbose_name = "film"
verbose_name_plural = "films"
def __unicode__(self):
return '%s (%s)' % (self.title, self.director)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
act = self.actors.all()
if act.count > 0:
self.actors_as_text = " ".join([actor.name for actor in act])
super(Film, self).save(*args, **kwargs)