Save M2M relations on form save (#13)

This commit is contained in:
Iwo Herka 2018-07-13 14:06:21 +00:00
parent ab9636a1c5
commit 8db7c39bbf
5 changed files with 26 additions and 4 deletions

View file

@ -99,5 +99,6 @@ class BaseDynamicEntityForm(ModelForm):
# Save entity and its attributes.
if commit:
instance.save()
self._save_m2m()
return instance

View file

@ -3,7 +3,6 @@ Managers.
This module contains the custom manager used by entities registered with eav.
'''
from django.db import models
from .queryset import EavQuerySet

View file

@ -25,7 +25,6 @@ class EavConfig(object):
GenericRelation from Entity to Value. None by default. Therefore,
if not overridden, it is not possible to query Values by Entities.
'''
manager_attr = 'objects'
manager_only = False
eav_attr = 'eav'

View file

@ -3,7 +3,7 @@ from django.contrib.admin.sites import AdminSite
import eav
from eav.admin import *
from .models import Patient
from .models import Patient, M2MModel, ExampleModel
from eav.models import Attribute
from eav.forms import BaseDynamicEntityForm
from django.contrib import admin
@ -44,6 +44,12 @@ class PatientForm(ModelForm):
fields = '__all__'
class M2MModelForm(ModelForm):
class Meta:
model = M2MModel
fields = '__all__'
class Forms(TestCase):
def setUp(self):
eav.register(Patient)
@ -75,4 +81,12 @@ class Forms(TestCase):
def test_invalid_submit(self):
form = PatientForm(dict(color='Blue'), instance=self.instance)
with self.assertRaises(ValueError):
jim = form.save()
jim = form.save()
def test_m2m(self):
m2mmodel = M2MModel.objects.create(name='name')
model = ExampleModel.objects.create(name='name')
form = M2MModelForm(dict(name='Lorem', models=[model.pk]), instance=m2mmodel)
form.save()
self.assertEqual(len(m2mmodel.models.all()), 1)

View file

@ -29,3 +29,12 @@ class ExampleModel(models.Model):
def __unicode__(self):
return self.name
@register_eav()
class M2MModel(models.Model):
name = models.CharField(max_length=12)
models = models.ManyToManyField(ExampleModel)
def __unicode__(self):
return self.name