Is the word start a usable field name in a Django model?
Question
I have a model called Order with a datetime field called start. I can read and write from/to that field no problem.
However, I just created a ModelForm and specified start as one of the fields=() in Meta and I get:
Unknown field(s) (start) specified for Order
I've made sure it is not a typo by copying and pasting the field name. If I remove that field, it works.
Here is the exact ModelForm
class OrderForm(ModelForm):
class Meta:
model = Order
fields = ('details', 'start', 'quantity', 'total')
EDIT added more details:
I tried using exclude = () to exclude all fields except those I need, and start does not appear in the form even though I don't exclude it.
Here is the model:
class Order(MyModel):
user = models.ForeignKey(User, )
invoice = models.ForeignKey(Invoice, )
unit_price = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True, )
subtotal = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null =True, )
tax = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True, )
misc = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True, )
total = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True, )
start = models.DateTimeField(auto_now_add=True, blank=True, null=True, )
end = models.DateTimeField(editable=True, blank=True, null=True, )
duration = models.PositiveSmallIntegerField(blank=True, null=True, )
quantity = models.PositiveSmallIntegerField(blank=True, null=True, )
notes = models.CharField(max_length=256, blank=True, null=True, )
details = models.CharField(max_length=64, blank=True, null=True, )
configured = models.BooleanField(default=False, )
Solution 2
I removed the auto_now_add=True and the problem is solved.
Thanks for everyone's help.
OTHER TIPS
Remove:
auto_now_add=True
Model field reference | Django documentation | Django :
As currently implemented, setting auto_now or auto_now_add to True will cause the field to have editable=False and blank=True set.
Maybe you have editable=False
defined for the start
field?
According to documentation:
If
False
, the field will not be displayed in the admin or any otherModelForm
. Default isTrue
.