Reputation: 914
I want to call a field from another model. I am doing by
def get_shift_date(self):
for d in Shiftdate.objects.select_related('sit_date'):
d.shift_date=self.sit_date
return d.shift_date
from
class Shiftdate(models.Model):
shift_date = models.DateField(blank=False,unique=True)
sit_date = models.ForeignKey(Sitting,
on_delete=models.CASCADE)
to
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)
But it didn't work. Could some one help me to correctly call shift date in the get_shift_date method?
Edit: Save method:
def save(self, *args, **kwargs):
self.sit_date = self.get_shift_date()
super(Sitting, self).save(*args, **kwargs)
Upvotes: 0
Views: 525
Reputation: 43300
You need to remove the self.
, d is the object that is currently being iterated over.
for d in Shiftdate.objects.select_related('sit_date'):
d.shift_date=self.sit_date
Note:
You won't actually be storing the value in the models since you don't save()
it
Your return won't work since its outside the loop and even then it will only return the first.
Upvotes: 1