Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango How to make a ForeignKey use a default?
    text
    copied!<p>I having a problem getting a ForeignKey to default to zero, it keeps telling me that the field is required, or if I try a different iteration it'll complain the field cannot be null.</p> <p>I am not sure what I am doing wrong, but I need the ForeignKey to be defaulted to zero if its null.</p> <p>I am experimenting with an idea to hold all contracts between several different entities (ie: companies and employees, companies and sponsors, etc, companies and suppliers, etc).</p> <p>The relationships are all working, however my ForeignKey statements are not letting me use a default of 0 (zero).</p> <pre><code># relationships # Only the company field should be required, the others are optional # This is so you can link a company with either a sponsor OR a worker # sponsor = models.ForeignKey('Sponsor', null=True, default=0, blank=False) company = models.ForeignKey('Company') worker = models.ForeignKey('Worker', null=False, default=0, blank=False) # possible future relationships (not implemented) #suppliers = models.ForeignKey('Supplier', null=True, default=0, blank=False) </code></pre> <p>Ideally, I would like it so that only the company field is required, and the others are purely optional and will default to zero.</p> <p>The idea is to have 1 table to rule/manage all the contracts between different entities.</p> <blockquote> <p>Pseduocode</p> <p>Contracts; id = 1 | company_id = 1 | sponsor_id = 0 | worker_id = 1 | length = 10 | salary = 10000</p> <p>// etc</p> </blockquote> <p>I have tried the following iterations.</p> <pre><code># models.ForeignKey('Sponsor', null=False, default=0, blank=True) # This makes the field required # models.ForeignKey('Sponsor', null=True, default=0, blank=True) # This complains that "Contracts.sponsor" does not allow null values. </code></pre> <p>Any pointers on how to make a make a ForeignKey use a default would be greatly appreciated.</p> <p>Many thanks.</p> <p>Edit. Adding the code that is causing the errors; This is a cut-down version of the code.</p> <pre><code>// company.py class Company(models.Model): """(Company description)""" sponsorContracts = models.ManyToManyField('Sponsor', through='Contracts', db_column='sponsor_id') employees = models.ManyToManyField('Worker', through='Contracts', db_column='worker_id') // contracts.py class Contracts(models.Model): """(Contracts description)""" # relationships sponsor = models.ForeignKey('Sponsor', null=True, default=0, blank=True) company = models.ForeignKey('Company') worker = models.ForeignKey('Worker', null=False, default=0, blank=False) // worker.py class Worker(models.Model): """(A list of employees)""" employer = models.ManyToManyField('Company', through='Contracts', db_column='company_id') // sponsor.py class Sponsor(models.Model): """(A list of sponsors)""" sponsorContracts = models.ManyToManyField('Company', through='SponsorContracts') </code></pre>
 

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