Note that there are some explanatory texts on larger screens.

plurals
  1. POCan a Django model field's default value be defined by a function dependent on a foreign parent model?
    primarykey
    data
    text
    <p>I'm trying to have the default value of <code>Report</code>'s fee be based on a parent model's attributes. I don't want to do this in <code>save()</code>, because the field needs to be presented to the user if they choose to override the value before saving.</p> <p>Here are the three methods I've tried, in addition to passing only function pointers (i.e. without <code>()</code>). The errors are raised when I run <code>python manage.py shell</code>.</p> <pre><code>#1 from django.db import models class Job(models.Model): veryImportant = models.IntegerField() def get_fee(self): return 2 * self.veryImportant class Report(models.Model): job = models.ForeignKey(Job) overridableFee = models.DecimalField(default=job.get_fee(), max_digits=7, decimal_places=2) #gives... #AttributeError: 'ForeignKey' object has no attribute 'get_fee' #2 from django.db import models class Job(models.Model): veryImportant = models.IntegerField() class Report(models.Model): job = models.ForeignKey(Job) overridableFee = models.DecimalField(default=self.set_fee(), max_digits=7, decimal_places=2) def set_fee(self): overridableFee = 2 * self.job.veryImportant #gives... #NameError: name 'self' is not defined #3 from django.db import models class Job(models.Model): veryImportant = models.IntegerField() def get_fee(): return 2 * veryImportant class Report(models.Model): job = models.ForeignKey(Job) overridableFee = models.DecimalField(max_digits=7, decimal_places=2) def __init__(self, *args, **kwargs): self.overridableFee = self.job.get_fee() super(models.Model, self).__init__(*args, **kwargs) #gives... #TypeError: object.__init__() takes no parameters </code></pre> <p>The error with #3 could be that I just have no idea how to override <strong>init</strong> properly. I copied something out of another answer and gave up after it didn't work.</p> <p>If none of these will work, I can always revert to just setting the fee after I create each Report in the view, but I'd rather do it automatically when the Report is created, for sanity.</p> <p>EDIT:</p> <p>Ended up going with #3, fixed with Yuji's answer and modified to make sure any possible fee set from the shell overrides what job.get_fee() wants.</p> <pre><code>def __init__(self, *args, **kwargs): super(Report, self).__init__(*args, **kwargs) if self.overridableFee == None and self.job and not self.pk: self.overridableFee = self.job.get_fee() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload