wproduct list 增加order
This commit is contained in:
parent
deb5eb1a3b
commit
444914fde9
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 3.2.9 on 2022-01-24 05:44
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('sam', '0010_auto_20211208_1408'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='order',
|
||||
name='need_mtest',
|
||||
field=models.BooleanField(default=False, verbose_name='是否需要军检'),
|
||||
),
|
||||
]
|
|
@ -157,6 +157,7 @@ class WProductListSerializer(serializers.ModelSerializer):
|
|||
warehouse_ = WareHouseSimpleSerializer(source='warehouse', read_only=True)
|
||||
children = serializers.SerializerMethodField()
|
||||
to_order_ = OrderSimpleSerializer(source='to_order', read_only=True)
|
||||
order_ = OrderSimpleSerializer(source='subproduction_plan__production_plan__order', read_only=True)
|
||||
class Meta:
|
||||
model = WProduct
|
||||
fields = '__all__'
|
||||
|
|
|
@ -148,7 +148,8 @@ class WProductViewSet(ListModelMixin, RetrieveModelMixin, GenericViewSet):
|
|||
"""
|
||||
perms_map = {'*': '*'}
|
||||
queryset = WProduct.objects.select_related('step', 'material',
|
||||
'subproduction_plan', 'warehouse', 'to_order').prefetch_related('wproduct_child')
|
||||
'subproduction_plan', 'warehouse', 'subproduction_plan__production_plan__order',
|
||||
'to_order').prefetch_related('wproduct_child')
|
||||
serializer_class = WProductListSerializer
|
||||
filterset_class = WProductFilterSet
|
||||
search_fields = ['number']
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
"""
|
||||
Mixin to dynamically select only a subset of fields per DRF resource.
|
||||
"""
|
||||
import warnings
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class DynamicFieldsMixin(object):
|
||||
"""
|
||||
A serializer mixin that takes an additional `fields` argument that controls
|
||||
which fields should be displayed.
|
||||
"""
|
||||
|
||||
@property
|
||||
def fields(self):
|
||||
"""
|
||||
Filters the fields according to the `fields` query parameter.
|
||||
A blank `fields` parameter (?fields) will remove all fields. Not
|
||||
passing `fields` will pass all fields individual fields are comma
|
||||
separated (?fields=id,name,url,email).
|
||||
"""
|
||||
fields = super(DynamicFieldsMixin, self).fields
|
||||
|
||||
if not hasattr(self, '_context'):
|
||||
# We are being called before a request cycle
|
||||
return fields
|
||||
|
||||
# Only filter if this is the root serializer, or if the parent is the
|
||||
# root serializer with many=True
|
||||
is_root = self.root == self
|
||||
parent_is_list_root = self.parent == self.root and getattr(self.parent, 'many', False)
|
||||
if not (is_root or parent_is_list_root):
|
||||
return fields
|
||||
|
||||
try:
|
||||
request = self.context['request']
|
||||
except KeyError:
|
||||
conf = getattr(settings, 'DRF_DYNAMIC_FIELDS', {})
|
||||
if not conf.get('SUPPRESS_CONTEXT_WARNING', False) is True:
|
||||
warnings.warn('Context does not have access to request. '
|
||||
'See README for more information.')
|
||||
return fields
|
||||
|
||||
# NOTE: drf test framework builds a request object where the query
|
||||
# parameters are found under the GET attribute.
|
||||
params = getattr(
|
||||
request, 'query_params', getattr(request, 'GET', None)
|
||||
)
|
||||
if params is None:
|
||||
warnings.warn('Request object does not contain query paramters')
|
||||
|
||||
try:
|
||||
filter_fields = params.get('fields', None).split(',')
|
||||
except AttributeError:
|
||||
filter_fields = None
|
||||
|
||||
try:
|
||||
omit_fields = params.get('omit', None).split(',')
|
||||
except AttributeError:
|
||||
omit_fields = []
|
||||
|
||||
# Drop any fields that are not specified in the `fields` argument.
|
||||
existing = set(fields.keys())
|
||||
if filter_fields is None:
|
||||
# no fields param given, don't filter.
|
||||
allowed = existing
|
||||
else:
|
||||
allowed = set(filter(None, filter_fields))
|
||||
|
||||
# omit fields in the `omit` argument.
|
||||
omitted = set(filter(None, omit_fields))
|
||||
|
||||
for field in existing:
|
||||
|
||||
if field not in allowed:
|
||||
fields.pop(field, None)
|
||||
|
||||
if field in omitted:
|
||||
fields.pop(field, None)
|
||||
|
||||
return fields
|
Loading…
Reference in New Issue