mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Fixes #1921: Ignore ManyToManyFields when validating a new object created via the API
This commit is contained in:
@ -5,6 +5,7 @@ import pytz
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db.models import ManyToManyField
|
||||
from django.http import Http404
|
||||
from rest_framework import mixins
|
||||
from rest_framework.exceptions import APIException
|
||||
@ -51,6 +52,11 @@ class ValidatedModelSerializer(ModelSerializer):
|
||||
|
||||
# Run clean() on an instance of the model
|
||||
if self.instance is None:
|
||||
model = self.Meta.model
|
||||
# Ignore ManyToManyFields for new instances (a PK is needed for validation)
|
||||
for field in model._meta.get_fields():
|
||||
if isinstance(field, ManyToManyField):
|
||||
attrs.pop(field.name)
|
||||
instance = self.Meta.model(**attrs)
|
||||
else:
|
||||
instance = self.instance
|
||||
|
Reference in New Issue
Block a user