11๐
โ
You just need to edit your save method in your form,
def save(self, *args, **kwargs):
if not commit:
raise NotImplementedError("Can't create User and Userextended without database save")
user = super().save(*args, **kwargs)
user_profile = Userextended(user=user, cristin=self.cleaned_data['cristin'])
user_profile.save()
user_profile.rolle.add(self.cleaned_data['rolle'])
user_profile.save()
return user
You need to save your UserExtended
model first, then add the Rolle
instances to the many to many relation.
๐คzaidfazil
1๐
If youโre using DRF you can use the validate method:
instead of this
def create(self, validated_data):
validated_data['slug'] = slugify(validated_data['name'])
return budgets.models.BudgetCategory.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.slug = slugify(validated_data['name'])
instance.save()
return instance
do this
def validate(self, data):
data['slug'] = slugify(data['name'])
return data
๐คRio Weber
- Celery โ No module named five
- Is there a command for creating an app using cookiecutter-django?
- Python: How can I override one module in a package with a modified version that lives outside the package?
0๐
The error message happened because โrolleโ should be specified as a many-to-one relationship and not a many-to-many. The model has been updated to the following and is now working perfectly:
class Userextended(models.Model):
...
rolle = models.ForeignKey(Personrolle, models.SET_NULL, blank=True, null=True)
๐คChristian
- ForeignKey to a model that is defined after/below the current model
- Django 1.9 JSONField order_by
- How do I get the django HttpRequest from a django rest framework Request?
- How to clear all session variables without getting logged out
Source:stackexchange.com