开发者

How to write a custom save method for a table to add data to a different table?

I am very new to django. I am trying to implement small application for my business which is a rental service for a football field which is made of artificial grass. Service is based on hourly rental of the field. People can rent the field hourly or they can chose membership option which includes specific day and hour in a week for a defined period. I have two classes in my model.

One is for one-time reservation. People can reserve the football field for any day and hour in a week. This is very simple.

class Reservation(models.Model):
    name = models.ForeignKey(User)
    membership = models.ForeignKey((Member), blank=True, null=True, editable=False)
    date = models.DateField()
    field_name = models.ForeignKey(Field)
    fee = models.DecimalField(decimal_places=0, max_digits=3)
    is_member = models.BooleanField(default=False, editable=False)
    is_active = models.BooleanField(default=True)

    def __unicode__(self):
        return u'%s %s %s %s' % (self.date, self.user.first_name,self.user.last_name, self.field)

    class Meta:
        unique_together = (("date", "field"))

Another for membership. People can apply to become a member. When somebody apply for a member ship, i want to create reservations for that day and hour between start and end dates automatically so there will not be any one-time reservation at that day and hour. I need to write a custom save method but i could not find how to add information to reservation table while saving membership table. I want to add member name and make is_member field to true in order to see that it is a member reservation.

class Member(models.Model):
    DAY_CHOICES = (
        (1, 'PAZARTESI'),
        (2, 'SALI'),
        (3, 'CARSAMBA'),
        (4, 'PERSEMBE'),
        (5, 'CUMA'),
        (6 ,'CUMARTESI'),
        (7, 'PAZAR'),
    )
    name = models.ForeignKey(User)
    starting_date = models.DateField()
    end_date = models.DateField()
    day = models.IntegerField(max开发者_如何学运维_length=1, choices=GUN_CHOICES)
    field = models.ForeignKey(Field)
    fee = models.DecimalField(decimal_places=0, max_digits=3)

    def __unicode__(self):
        return u'%s %s, %s, %s' % (self.name.first_name, self.name.last_name, self.day, self.field)

    class Meta:
        unique_together = (('date', 'field'))

Could you please help?

Thanks.


Instead of overriding the save method you could use a post_save signal.

The basic usage is explained here: http://www.djangofoo.com/85/signal-connect-disconnect-django-signals.

So every time after a member is saved you could call a function that creates an appropriate reservation.

Tweaking the example ...

def create_reservation(sender, **kwargs):
    # the object which is saved can be accessed via kwargs 'instance' key.
    member = kwargs['instance']
    print 'the object is now saved.'
    # ...create reservation...
    reservation = Reservation()
    reservation.is_member = True
    reservation.membership = member
    ....
    reservation.save()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜