python - Django one line datetime if null -
i'd this:
class ventegroupee(models.model): date_v_debut = models.datetimefield( default=datetime.now, editable=true ) date_v_fin = models.datetimefield( default=none, null=true, editable=true, blank=true ) def __str__(self): return "{0} - ({1} -> {2})".format( self.id, dateformat(self.date_v_debut).format('js f y h:i') if self.date_v_debut else 'infinity', dateformat(self.date_v_fin).format('js f y h:i') if self.date_v_fin else 'infinity' )
but 2 last lines ugly (date_v_debut
, date_v_fin
may null need check). there nice "pythonic"/"djangostic" way of doing this?
you create helper function format date.
def format_date(value) return dateformat(value).format('js f y h:i') if value else 'infinity'
then __str__
method simplifies to
def __str__(self): return "{0} - ({1} -> {2})".format( self.id, format_date(self.date_v_debut), format_date(self.date_v_fin), )
Comments
Post a Comment