where to hash form data before saving the model in createview in Django? -


I am a bit confused where the formal / model field can be validated in Generic Creatives / Update Weave. Below on my imaginary models, I have to be washed using field custom secret using my custom hash function and some validation has been assumed for the secret field (examples below I have options to do this:

1) Method of saving method (I did not show it below)

2) Method of saving form In (I have shown below) <

3) form_valid method 4) How can I enter cleaned_data in the generic view ( cleaned data only code> form_valid )

Which is the correct way to do this, both sides and the opposition I will use the same form for the updateView In that case, I will ignore the secret before displaying its data on the form. Where should it be?

My model:

  class account (models.Model): user = models.ForeignKey (user) created = models.DateField (auto_now) = True = models.IntegerField ()   

My form:

  AccountCreateForm (forms. Secret = forms .Keyfield (max_language = 100) class meta: model = media content excluded = (secret, user, created) def save (self, user, debate): obj = super (AccountCreateView, self). = False) obj.user = self.cleaned_data ['user'] obj.secret = myHashfunction (self.cleaned_data ['secret']) obj.save ()   

See my:

  Category AccountCreateView (CreateView): "" "Displays form to create a new search" "model = account profiles-accountCreateForm success_url = reverse_lazy ('Home') template_name = 'app / account_form.html' def form_valid (auto, form): f = form.save (commit = false) secret = myHashfunction (self.request.POST ['secret']) f.user = self.request.user f.sav E () Returns Super (Account cutviewview, self) .form_valid (form)   

Edit:

Please refer to my model and View form in the edit field in the form I use in the form Setr not. This is a new area, which takes CharField, but the model delivers it as IntegerField. My hashfankton has converted four fields into intifield.

I think that in this case form better than ModelForm , leaving each field on your model, makes it redundant, then you will not have any additional verification for the un-hashed secret with clean_secret should do. AccountCreateForm (forms.Form): secret = Now, if you are not using ModelForm , then I can suggest more than FormView . CreateView Normal CreateView has been reduced by a good fit. AccountCreateView (FormView): Form displays to show a new form "" "form_class = AccountCreateForm success_url = reverse_lazy ('home') template_name = 'app / account_form_h 'DIF form_widiD (self, form): unhash_acent = form.clluted_data [' right '] hashed_screk = myhashfunction (unauthorized_secret) user = self.request.user # maybe to check some extra logic here whether the user already exists, B The breakfast. Abyssites.Sent (user = user, secret = ishead_sweet,) return super (account credit scene, self). Form_valid (form)

Comments

Popular posts from this blog

jasper reports - How to center align barcode using jasperreports and barcode4j -

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -