I wish to hide a date field from the user, but process it as part of a form so that I know what date the form was sent. I also wish to display this date under the Django admin.
models.py
class Purchase(models.Model):
payment_type = models.ForeignKey('CardChoices', related_name='Payment Types')
card_name = models.CharField(max_length=26, default=None, validators=[alphanumeric_RE])
card_number = models.CharField(max_length=19, default=None)
security_code = models.IntegerField(max_length=3, default=None)
expiry_date = models.DateField(default=datetime.now)
date = models.DateField(auto_now=True, default=datetime.now)
def __str__(self):
return self.card_name
class Meta:
ordering = [ "date" ]
forms.py
class PurchaseForm(forms.ModelForm):
date = forms.DateField(widget=extras.SelectDateWidget, initial=date.today)
class Meta:
model = Purchase
fields = "__all__"
I've tried hiding the field under Meta using the exclude tag, and only adding the fields that I need under fields, but it still displays all of them. I've tried using forms.HiddenInput() which does hide the input, but it also hides the value from the admin. If my understanding is correct, I can't use auto_now_add on a form either. Any suggestions?
Aucun commentaire:
Enregistrer un commentaire