python - How to hide a field in django modelform? -


for example:

class testmodel(models.model):     ref1 = models.foreignkey(refmodel)     text1 = models.textfield()  class testmodelform(modelform):     class meta:         model = testmodel         fields = ('text1') 

i allow user input text1 field, when redefine post method of view, want set ref1 value, how should that?

i wish can let testmodelform has ref1 field don't let user modify it, can modify value of request.post in post method, , pass testmodelform, possible?

you can use hiddeninput ref1 widget:

class testmodelform(modelform):     class meta:         model = testmodel         widgets = {             'ref1': forms.hiddeninput(),         } 

another option saving form commit argument equal false. way can include visible fields in form , update model instance needed data:

def some_view(request):     # ...     if request.method == 'post':         form = testmodelform(request.post)         if form.is_valid():             instance = form.save(commit=false)             ref = get_ref_according_to_url()             instance.ref1 = ref             instance.save()             # ... 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -