mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-30 04:26:44 +00:00
Simpler implementation
This commit is contained in:
parent
8913b74f41
commit
232899e0c4
@ -7,6 +7,8 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from collections import OrderedDict
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.core.exceptions import ValidationError as DjangoValidationError
|
from django.core.exceptions import ValidationError as DjangoValidationError
|
||||||
@ -42,6 +44,38 @@ class InvenTreeModelSerializer(serializers.ModelSerializer):
|
|||||||
but also ensures that the underlying model class data are checked on validation.
|
but also ensures that the underlying model class data are checked on validation.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def __init__(self, instance=None, data=empty, **kwargs):
|
||||||
|
|
||||||
|
self.instance = instance
|
||||||
|
|
||||||
|
# If instance is None, we are creating a new instance
|
||||||
|
if instance is None:
|
||||||
|
|
||||||
|
if data is empty:
|
||||||
|
data = OrderedDict()
|
||||||
|
else:
|
||||||
|
# Required to side-step immutability of a QueryDict
|
||||||
|
data = data.copy()
|
||||||
|
|
||||||
|
# Add missing fields which have default values
|
||||||
|
ModelClass = self.Meta.model
|
||||||
|
|
||||||
|
fields = model_meta.get_field_info(ModelClass)
|
||||||
|
|
||||||
|
for field_name, field in fields.fields.items():
|
||||||
|
|
||||||
|
if field.has_default() and field_name not in data:
|
||||||
|
|
||||||
|
value = field.default
|
||||||
|
|
||||||
|
# Account for callable functions
|
||||||
|
if callable(value):
|
||||||
|
value = value()
|
||||||
|
|
||||||
|
data[field_name] = value
|
||||||
|
|
||||||
|
super().__init__(instance, data, **kwargs)
|
||||||
|
|
||||||
def get_initial(self):
|
def get_initial(self):
|
||||||
"""
|
"""
|
||||||
Construct initial data for the serializer.
|
Construct initial data for the serializer.
|
||||||
@ -50,6 +84,8 @@ class InvenTreeModelSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
initials = super().get_initial()
|
initials = super().get_initial()
|
||||||
|
|
||||||
|
print("initials:", initials)
|
||||||
|
|
||||||
# Are we creating a new instance?
|
# Are we creating a new instance?
|
||||||
if self.instance is None:
|
if self.instance is None:
|
||||||
ModelClass = self.Meta.model
|
ModelClass = self.Meta.model
|
||||||
@ -70,35 +106,6 @@ class InvenTreeModelSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
return initials
|
return initials
|
||||||
|
|
||||||
def is_valid(self, raise_exception=False):
|
|
||||||
"""
|
|
||||||
Also override the is_valid() method, as in some cases get_initial() is not actually called.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Calling super().is_valid creates self._validated_data
|
|
||||||
valid = super().is_valid(raise_exception)
|
|
||||||
|
|
||||||
# Are we creating a new instance?
|
|
||||||
if self.instance is None:
|
|
||||||
ModelClass = self.Meta.model
|
|
||||||
|
|
||||||
fields = model_meta.get_field_info(ModelClass)
|
|
||||||
|
|
||||||
for field_name, field in fields.fields.items():
|
|
||||||
|
|
||||||
if field.has_default():
|
|
||||||
if field_name not in self._validated_data.keys():
|
|
||||||
|
|
||||||
value = field.default
|
|
||||||
|
|
||||||
# Account for callable functions
|
|
||||||
if callable(value):
|
|
||||||
value = value()
|
|
||||||
|
|
||||||
self._validated_data[field_name] = value
|
|
||||||
|
|
||||||
return valid
|
|
||||||
|
|
||||||
def run_validation(self, data=empty):
|
def run_validation(self, data=empty):
|
||||||
""" Perform serializer validation.
|
""" Perform serializer validation.
|
||||||
In addition to running validators on the serializer fields,
|
In addition to running validators on the serializer fields,
|
||||||
|
@ -366,7 +366,6 @@ class PartAPITest(InvenTreeAPITestCase):
|
|||||||
self.assertFalse(response.data['purchaseable'])
|
self.assertFalse(response.data['purchaseable'])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class PartDetailTests(InvenTreeAPITestCase):
|
class PartDetailTests(InvenTreeAPITestCase):
|
||||||
"""
|
"""
|
||||||
Test that we can create / edit / delete Part objects via the API
|
Test that we can create / edit / delete Part objects via the API
|
||||||
|
Loading…
x
Reference in New Issue
Block a user