Django How to make a ForeignKey use a default?
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.
I am not sure what I am doing wrong, but I need the ForeignKey to be defaulted to zero if its null.
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).
The relationsh开发者_如何学运维ips are all working, however my ForeignKey statements are not letting me use a default of 0 (zero).
# 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)
Ideally, I would like it so that only the company field is required, and the others are purely optional and will default to zero.
The idea is to have 1 table to rule/manage all the contracts between different entities.
Pseduocode
Contracts; id = 1 | company_id = 1 | sponsor_id = 0 | worker_id = 1 | length = 10 | salary = 10000
// etc
I have tried the following iterations.
# 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.
Any pointers on how to make a make a ForeignKey use a default would be greatly appreciated.
Many thanks.
Edit. Adding the code that is causing the errors; This is a cut-down version of the 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')
A relationship between entities either exists (valid reference to id in another table), or it doesn't (value undefined - null). If you truly 'need' a non-null value in there that doesn't reference a valid entity, you could try having a 'default' entity defined in the referenced table, and define the default value to that.
I'd question the validity of requiring a non-null default FK value however in most cases. Though I have done something similar in one or two cases:
User(id, username, gender_id)
Gender(id, value)
Gender:
id 1 value unknown
id 2 value male
id 3 value female
Really though, null is known as value undefined and should be used as such in the majority of cases.
How can a ForeignKey default to 0? The whole point of a FK is that it is constrained to the values of a key in another table. There can't be a row in that table with ID of 0, so 0 is not a valid value for the FK.
If you want your model instances to have a relationship with one of several other tables, you probably want Generic relations.
精彩评论