MCPcopy Create free account
hub / github.com/encode/django-rest-framework / get_relation_kwargs

Function get_relation_kwargs

rest_framework/utils/field_mapping.py:250–304  ·  view source on GitHub ↗

Creates a default instance of a flat relational field.

(field_name, relation_info)

Source from the content-addressed store, hash-verified

248
249
250def get_relation_kwargs(field_name, relation_info):
251 """
252 Creates a default instance of a flat relational field.
253 """
254 model_field, related_model, to_many, to_field, has_through_model, reverse = relation_info
255 kwargs = {
256 'queryset': related_model._default_manager,
257 'view_name': get_detail_view_name(related_model)
258 }
259
260 if to_many:
261 kwargs['many'] = True
262
263 if to_field:
264 kwargs['to_field'] = to_field
265
266 limit_choices_to = model_field and model_field.get_limit_choices_to()
267 if limit_choices_to:
268 if not isinstance(limit_choices_to, models.Q):
269 limit_choices_to = models.Q(**limit_choices_to)
270 kwargs['queryset'] = kwargs['queryset'].filter(limit_choices_to)
271
272 if has_through_model:
273 kwargs['read_only'] = True
274 kwargs.pop('queryset', None)
275
276 if model_field:
277 if model_field.verbose_name and needs_label(model_field, field_name):
278 kwargs['label'] = capfirst(model_field.verbose_name)
279 help_text = model_field.help_text
280 if help_text:
281 kwargs['help_text'] = help_text
282 if not model_field.editable:
283 kwargs['read_only'] = True
284 kwargs.pop('queryset', None)
285 if model_field.null:
286 kwargs['allow_null'] = True
287 if kwargs.get('read_only', False):
288 # If this field is read-only, then return early.
289 # No further keyword arguments are valid.
290 return kwargs
291
292 if model_field.has_default() or model_field.blank or model_field.null:
293 kwargs['required'] = False
294 if model_field.validators:
295 kwargs['validators'] = model_field.validators
296 if getattr(model_field, 'unique', False):
297 validator = UniqueValidator(
298 queryset=model_field.model._default_manager,
299 message=get_unique_error_message(model_field))
300 kwargs['validators'] = kwargs.get('validators', []) + [validator]
301 if to_many and not model_field.blank:
302 kwargs['allow_empty'] = False
303
304 return kwargs
305
306
307def get_nested_relation_kwargs(relation_info):

Callers 1

Calls 6

UniqueValidatorClass · 0.90
get_detail_view_nameFunction · 0.85
needs_labelFunction · 0.85
get_unique_error_messageFunction · 0.85
filterMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected