How to save one field value from a model to another model field

25 views
Skip to first unread message

Md. Ohiduzzaman

unread,
Aug 16, 2016, 1:49:32 PM8/16/16
to Django users

I designed two models: one model for storing Sitting date and another mode for storing Shifting date. Two model are linked by foreign key. Here is my two models:

class Sitting(models.Model):
    sit_date = models.DateField(blank=False,unique=True)
    cut_off_date = models.DateField(null=True, blank=True)
    ballot_date = models.DateField(null=True, blank=True)
    sess_no = models.ForeignKey(Session,
                                 on_delete=models.CASCADE)
    genre = TreeForeignKey('Genre', null=True, blank=True, db_index=True)

Shift Model:

 class Shiftdate(models.Model):
    shift_date = models.DateField(blank=False,unique=True)
    sit_date = models.ForeignKey(Sitting,
                                on_delete=models.CASCADE)

Using Shift model's shift_date, I changed existing sitting dates to other dates as:

Sitting Date     Shifting Date
2016-09-01       Sept. 15, 2016
2016-09-08       Sept. 19, 2016


Now I want to make these shift date to a new sitting dates.  For this I define following method in Sitting model:


def get_shift_date(self):
            return self.sit_date= self.shift_date 

And this shift_date to be saved in the Sitting model in sit_date field. I define following save method:

 def save(self, *args, **kwargs):  
            self.sit_date = self.get_shift_date()
        super(Sitting, self).save(*args, **kwargs)

But this did not return shift dates as sitting dates. Could someone help me to fix this codes?

Vinicius Assef

unread,
Aug 17, 2016, 10:28:38 AM8/17/16
to Django users
Your method get_shift_date() has a syntax error.

Your save() method has identation problem.

Please correct them and send us the right code.
Reply all
Reply to author
Forward
0 new messages