Traceback (most recent call last): File "/var/www/django-netjsonconfig/django_netjsonconfig/tests/test_device.py", line 106, in test_m2m_validation d.templates.add(t) File "/home/nemesis/.virtualenvs/djnetconfig3/lib/python3.4/site-packages/django/db/models/fields/related_descriptors.py", line 843, in add self._add_items(self.source_field_name, self.target_field_name, *objs) File "/home/nemesis/.virtualenvs/djnetconfig3/lib/python3.4/site-packages/sortedm2m/fields.py", line 138, in _add_items for val in vals: File "/home/nemesis/.virtualenvs/djnetconfig3/lib/python3.4/site-packages/django/db/models/query.py", line 258, in __iter__ self._fetch_all() File "/home/nemesis/.virtualenvs/djnetconfig3/lib/python3.4/site-packages/django/db/models/query.py", line 1074, in _fetch_all self._result_cache = list(self.iterator()) File "/home/nemesis/.virtualenvs/djnetconfig3/lib/python3.4/site-packages/django/db/models/query.py", line 158, in __iter__ for row in compiler.results_iter(): File "/home/nemesis/.virtualenvs/djnetconfig3/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 806, in results_iter results = self.execute_sql(MULTI) File "/home/nemesis/.virtualenvs/djnetconfig3/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 852, in execute_sql cursor.execute(sql, params) File "/home/nemesis/.virtualenvs/djnetconfig3/lib/python3.4/site-packages/django/db/backends/utils.py", line 59, in execute self.db.validate_no_broken_transaction() File "/home/nemesis/.virtualenvs/djnetconfig3/lib/python3.4/site-packages/django/db/backends/base/base.py", line 429, in validate_no_broken_transaction "An error occurred in the current transaction. You can't "django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/2e6e82d0-0645-4fd7-8905-d327c99b6352%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author, related_name="books")
lead_author = models.ForeignKey(Author, on_delete=models.CASCADE)
def clean(self):
if (self.id is None):
# Validate all the model fields that are not One2Many or Many2Many
pass
else:
# Validate all the One2Many or Many2Many fields
pass
class Chapter(models.Model):
title = models.CharField(max_length=100)
book = models.ForeignKey(Book, on_delete=models.CASCADE, related_name="chapters")
author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name="chapters")
class BookCreate(CreateView):
model = Book
fields = '__all__'
ChapterFormSet = inlineformset_factory(Book, Chapter, fields="__all__")
def post(self, request, *args, **kwargs):
self.form = self.get_form()
if self.form.is_valid():
try:
with transaction.atomic():
self.object = self.form.save(commit=True)
self.formset = self.ChapterFormSet(request.POST, request.FILES, instance=self.object)
self.formset.is_valid():
self.formset.save()
self.object.clean()
except IntegrityError:
transaction.set_rollback(True)
return self.form_invalid(self.form)
return self.form_valid(self.form)
else:
return self.form_invalid(self.form)
AUTH_PASSWORD_VALIDATORS
¶Default: []
(Empty list)
The list of validators that are used to check the strength of user’s passwords.
See Password validation for more details. By default, no validation is
performed and all passwords are accepted.
And propose:
TOMANY_RELATIONSHIP_VALIDATORS
¶Default: False
A boolean that specifies whether to use atomic transactions on in CreateView and UpdateView POST requests in order to support relationship (OneToMany and ManyToMany) validation in the Model itself. If true, Model.clean() is called as usual, then inside an atomic transaction all submitted forms and formsets are saved and Model.clean_relations() is called. It is important to note that when Model.clean() is saved ToMany relationship (creation or changes) are not visible to Model.clean() but are visible in Model.clean_relations() where they can be validated.
Implement the code in CreateView.post(...) and UpdateView.post(...) that does this, that is a little savvier than my example in previous mail, but if form.is_valid() pass opens a transaction, saves the form and then for each other form or formset in the POSTed data (not sure off hand how to find those, more learning for me), check each with their .is_valid() and if passing save it, then call Model.clean_relations() (for each model involved (associated with any of the submitted forms or formsets). Of course has to work if the model fails to implement clean_relations(), either by checking it's there first of defining a default implementation which pay just pass.
If Model.clean_relations() fails (throws an exception), roll back, if not commit.
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/239d7900-94a8-4491-b973-50e8731b3957%40googlegroups.com.