logo
down
shadow

Why does known valid Django model instance fail is_valid() after retrieval from database?


Why does known valid Django model instance fail is_valid() after retrieval from database?

By : user3851429
Date : October 16 2020, 11:12 PM
hope this fix your issue form.is_valid() verifies the form.data dict, which is sent via the constructor for Form(data=request.POST)
ModelForm.instance associates the data with a particular table row, so that a save necessarily performs an update and not an insert. This is also passed via the constructor.
code :


Share : facebook icon twitter icon
Django ModelForm is_valid saves the instance automatically

Django ModelForm is_valid saves the instance automatically


By : xiaocharlie
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further No form.is_valid() does not save the new data in DB. However, it updates the instance object with new attributes so that it can use them when you call form.save().
Here is how you can verify this:
code :
>>> mc = MyModel.objects.get(id=3)
>>> mf=MyModelForm({'name': 'abcd'}, instance=mc)
>>> mc.name
u'oldName'
>>> mf.is_valid()
True
>>> mc.name
u'abcd'
>>> mc2 = MyModel.objects.get(id=3)  #get the new instance from DB
>>> mc2.name
u'OldName'
>>> 
Django model form is_valid() in specific database

Django model form is_valid() in specific database


By : Chiu Fong
Date : March 29 2020, 07:55 AM
help you fix your problem To enable the router just add it to DATABASE_ROUTERS in your setting.py, see detailes here: https://docs.djangoproject.com/en/1.9/topics/db/multi-db/#using-routers
Every router method gets hints dictionary, which should contain instance key representing the model instance being used. Depending on what information you want to get from the view, you might be good with the information from instance attributes. See here: https://docs.djangoproject.com/en/1.9/topics/db/multi-db/#topics-db-multi-db-hints.
code :
def db_for_read(self, model, **hints):
    model_instance = hints.get('instance')   
    if model_instance is not None:
        pass
        #perform actions with model_instance attributes
    return None
class ProductForm(ModelForm):
    class Meta:
        ...

    def save(self, commit=True):
        product_to_save=super(ProductForm, self).save(commit=False)
        #the above creates product_to_save instance, but doesn't saves it to DB
        product_to_save.save(using=='banco1')
        return product_to_save
class ProductForm(ModelForm):
   category_field = forms.ModelMultipleChoiceField(queryset=Category.objects.using('banco1'))
    ...
class ProductForm(ModelForm):
    ...

    def __init__(self, *args, db_to_use='default', **kwargs):
        super(ProductForm, self).__init__(*args, **kwargs)
        #category = forms.ModelChoiceField(queryset=Category.objects.using(db_to_use).all())
        #the below line suggested as improvement by Alasdair and confirmed as working by Pavarine, so I updated the answer here
        self.fields['category'].queryset = Category.objects.using(database).all()
product_form = ProductForm(request.POST, db_to_use=request.session['database'])
Django formset factory ChoiceField causes is_valid to fail and return false

Django formset factory ChoiceField causes is_valid to fail and return false


By : Marcus Carlsson
Date : March 29 2020, 07:55 AM
To fix the issue you can do I'm using a formset factory to generate a dynamic set of form elements. I'd like to add a set of dynamic radio buttons and dropdown menus, but when I use ChoiceField, the search.is_valid() evaluates to false. I'm sure I'm doing something obviously wrong, but I can't find it. , You have:
code :
self.search_forms = search_formset(get_request or data)
self.search_forms = search_formset(get_request or None)
Django Model Formset ignore Form Instance on is_valid()

Django Model Formset ignore Form Instance on is_valid()


By : German Cishevskiy
Date : March 29 2020, 07:55 AM
hope this fix your issue No, Django will never use initial data in place of missing posted data - otherwise how could you ever use a form to set a field to empty? Instead, you should exclude that field from the form, in which case the existing instance value will be preserved.
Either do this explicitly in the Meta class of MyForm, or pass the exclude parameter to modelformset_factory.
While using django restframework Multiple database, serializer.is_valid() always go to default database for validation

While using django restframework Multiple database, serializer.is_valid() always go to default database for validation


By : Trong Nguyen
Date : March 29 2020, 07:55 AM
I hope this helps you . This is not the exact solution to above problem but we have 2 options.
1) Do not go for serializer.is_valid() or serializer.save(). Directly create ngroup:
Related Posts Related Posts :
  • Is this data appropriate for keeping in a database?
  • Efficient comparison of 1 million vectors containing (float, integer) tuples
  • How to Protect Sensible Passwords stored in a Database
  • Cakephp HABTM relation
  • conditional statements in derby
  • How to completely dump the data for Django-CMS
  • How to configure database permissions for a Django app?
  • ms-access: is it possible to change a combo box to text box using vba?
  • How should I store extremely large amounts of traffic data for easy retrieval?
  • Using Perl to select 1 from@db_link where db_link comes from each entries of a lookup table
  • Database with great read performance
  • Synchronizing one or more databases with a master database - Foreign keys
  • Would this SQL tuning technique work?
  • How can I change the database settings in openx-2.6.4?
  • Unit testing methods that call methods with dependencies to the data access layer
  • Fastest key-value pair database?
  • How to use my own sqlite database?
  • Data Versioning Advice
  • Android Pre-Populated Database
  • display/retrieve image from sql database in vb.net
  • How do you verify the correct data is in a data mart?
  • Reading HTML data from database is slow? Need a better approach?
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org