def contribute_to_class(self, cls, name): """ Add each of the names and fields in the ``fields`` attribute to the model the relationship field is applied to, and set up the related item save and delete signals for calling ``related_items_changed``. """ for field in cls._meta.many_to_many: if isinstance(field, self.__class__): e = "Multiple %s fields are not supported (%s.%s, %s.%s)" % ( self.__class__.__name__, cls.__name__, cls.__name__, name, field.name) raise ImproperlyConfigured(e) self.related_field_name = name super(BaseGenericRelation, self).contribute_to_class(cls, name) # Not applicable to abstract classes, and in fact will break. if not cls._meta.abstract: for (name_string, field) in self.fields.items(): if "%s" in name_string: name_string = name_string % name extant_fields = cls._meta._forward_fields_map if name_string in extant_fields: continue if field.verbose_name is None: field.verbose_name = self.verbose_name cls.add_to_class(name_string, copy(field)) # Add a getter function to the model we can use to retrieve # the field/manager by name. getter_name = "get_%s_name" % self.__class__.__name__.lower() cls.add_to_class(getter_name, lambda self: name) sender = get_related_model(self) post_save.connect(self._related_items_changed, sender=sender) post_delete.connect(self._related_items_changed, sender=sender)
def get_profile_user_fieldname(profile_model=None, user_model=None): """ Returns the name of the first field on the profile model that points to the ``auth.User`` model. """ Profile = profile_model or get_profile_model() User = user_model or get_user_model() for field in Profile._meta.get_fields(): if get_related_model(field) == User: return field.name raise ImproperlyConfigured("Value for ACCOUNTS_PROFILE_MODEL does not " "contain a ForeignKey field for auth.User: %s" % Profile.__name__)