mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Move model_to_dict() into assertInstanceEqual(); clean up test data
This commit is contained in:
@ -2,35 +2,6 @@ import logging
|
||||
from contextlib import contextmanager
|
||||
|
||||
from django.contrib.auth.models import Permission, User
|
||||
from django.forms.models import model_to_dict as _model_to_dict
|
||||
|
||||
|
||||
def model_to_dict(instance, fields=None, exclude=None):
|
||||
"""
|
||||
Customized wrapper for Django's built-in model_to_dict(). Does the following:
|
||||
- Excludes the instance ID field
|
||||
- Exclude any fields prepended with an underscore
|
||||
- Convert any assigned tags to a comma-separated string
|
||||
"""
|
||||
_exclude = ['id']
|
||||
if exclude is not None:
|
||||
_exclude += exclude
|
||||
|
||||
model_dict = _model_to_dict(instance, fields=fields, exclude=_exclude)
|
||||
|
||||
for key in list(model_dict.keys()):
|
||||
if key.startswith('_'):
|
||||
del model_dict[key]
|
||||
|
||||
# TODO: Differentiate between tags assigned to the instance and a M2M field for tags (ex: ConfigContext)
|
||||
elif key == 'tags':
|
||||
model_dict[key] = ','.join(sorted([tag.name for tag in model_dict['tags']]))
|
||||
|
||||
# Convert ManyToManyField to list of instance PKs
|
||||
elif model_dict[key] and type(model_dict[key]) in (list, tuple) and hasattr(model_dict[key][0], 'pk'):
|
||||
model_dict[key] = [obj.pk for obj in model_dict[key]]
|
||||
|
||||
return model_dict
|
||||
|
||||
|
||||
def post_data(data):
|
||||
|
Reference in New Issue
Block a user