
Question:
class SomeModel(models.Model): text = models.TextField() ip = models.IPAddressField() created_on = models.DateTimeField() updated_on = models.DateTimeField()
Say I have that as a model, what if I wanted to only display 'text' field widget for the user to submit data, but I obviously wouldn't want the user to see the widgets for ip,created_on, updated_on to be changed. Yes I know I can add it as a hidden field for the form, but that's not what I'm looking for.
I'm more wondering how can I not render those fields at all when rendering my form, and just dynamically populating the fields when a form is posted and pass form validation? I'm guessing to somehow override the blank values of ip,created_on,updated_on while the form is being cleaned/validated. I know how to do this in the view by using request.POST.copy and injected my values, but I'd like to know if it's possible in models or forms.
Solution:1
Two things:
First ModelForms:
Class SomeModelForm(ModelForm): class Meta: exclude = ['ip','created_on', 'updated_on']
Two Model Fields API:
class SomeModel(models.Model): text = models.TextField() ip = models.IPAddressField() created_on = models.DateTimeField(auto_now_add=True) updated_on = models.DateTimeField(auto_now=True)
Three:
For ip, i think you should to that in your views.
Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
EmoticonEmoticon