def _get_not_null_pk(self, definition, using): # XXX the below logic is required to get the expected results back # when querying for NULL values. since NULL can be a value and a # placeholder for non-existent values, then a condition to ensure # the row's primary key is also NOT NULL must be added. Django # assumes in all cases when querying on a NULL value all joins in # the chain up to that point must be promoted to LEFT OUTER JOINs, # which could be a reasonable assumption for some cases, but for # getting the existent rows of data back for our purposes, the # assumption is wrong. the conditions defined for the query are not # necessarily going to also be the data selected # if this field is already the primary key, then don't bother # adding this condition, since it would be redundant if definition.field.primary_key: return Q() from avocado.meta.models import Definition name = definition.model._meta.pk.name # instantiate a new object to utilize the shortcut methods _definition = Definition(app_name=definition.app_name, model_name=definition.model_name, field_name=name) key = _definition.query_string('isnull', using=using) return Q(**{key: False})
def handle_label(self, label, **options): "Handles app_label or app_label.model_label formats." labels = label.split(".") mods = None create_domains = options.get("create_domains") include_non_editable = options.get("include_non_editable") # a specific model is defined if len(labels) == 2: # attempts to find the model given the app and model labels model = models.get_model(*labels) if model is None: print 'Cannot find model "%s", skipping...' % label return mods = [model] # get all models for the app else: app = models.get_app(*labels) mods = models.get_models(app) if mods is None: print 'Cannot find app "%s", skipping...' % label return app_name = labels[0] for model in mods: cnt = 0 model_name = model._meta.object_name.lower() if create_domains: domain = self._get_domain(model) for field in model._meta.fields: # in most cases the primary key fields and non-editable will not # be necessary. editable usually include timestamps and such if isinstance(field, self.ignored_field_types): continue # ignore non-editable fields since in most cases they are for # managment purposes if not field.editable and not include_non_editable: continue kwargs = {"app_name": app_name.lower(), "model_name": model_name, "field_name": field.name} # do initial lookup to see if it already exists, skip if it does if Definition.objects.filter(**kwargs).exists(): print "%s.%s already exists. Skipping..." % (model_name, field.name) continue # add verbose name kwargs["name"] = field.verbose_name.title() definition = Definition(**kwargs) if create_domains: definition.domain = domain definition.published = False definition.save() cnt += 1 if cnt == 1: print "1 definition added for %s" % model_name else: print "%d definitions added for %s" % (cnt, model_name)