MCPcopy
hub / github.com/django/django / add_field

Method add_field

django/db/models/options.py:340–373  ·  view source on GitHub ↗
(self, field, private=False)

Source from the content-addressed store, hash-verified

338 self._expire_cache()
339
340 def add_field(self, field, private=False):
341 # Insert the given field in the order in which it was created, using
342 # the "creation_counter" attribute of the field.
343 # Move many-to-many related fields from self.fields into
344 # self.many_to_many.
345 if private:
346 self.private_fields.append(field)
347 elif field.is_relation and field.many_to_many:
348 bisect.insort(self.local_many_to_many, field)
349 else:
350 bisect.insort(self.local_fields, field)
351 self.setup_pk(field)
352
353 # If the field being added is a relation to another known field,
354 # expire the cache on this field and the forward cache on the field
355 # being referenced, because there will be new relationships in the
356 # cache. Otherwise, expire the cache of references *to* this field.
357 # The mechanism for getting at the related model is slightly odd -
358 # ideally, we'd just ask for field.related_model. However,
359 # related_model is a cached property, and all the models haven't been
360 # loaded yet, so we need to make sure we don't cache a string
361 # reference.
362 if (
363 field.is_relation
364 and hasattr(field.remote_field, "model")
365 and field.remote_field.model
366 ):
367 try:
368 field.remote_field.model._meta._expire_cache(forward=False)
369 except AttributeError:
370 pass
371 self._expire_cache()
372 else:
373 self._expire_cache(reverse=False)
374
375 def setup_pk(self, field):
376 if not self.pk and field.primary_key:

Calls 3

setup_pkMethod · 0.95
_expire_cacheMethod · 0.95
appendMethod · 0.45