+
+ def wikidata_populate_field(self, client, entity, attname, wd, save, lang):
+ model_field = self._meta.get_field(attname)
+ if isinstance(model_field, models.ManyToManyField):
+ if getattr(self, attname).all().exists():
+ return
+ else:
+ if getattr(self, attname):
+ return
+
+ wdvalue = None
+ if wd == "description":
+ wdvalue = entity.description.get(lang, str(entity.description))
+ elif wd == "label":
+ wdvalue = entity.label.get(lang, str(entity.label))
+ else:
+ try:
+ # TODO: lang?
+ wdvalue = entity.get(client.get(wd))
+ except DatavalueError:
+ pass
+
+ self.set_field_from_wikidata(attname, wdvalue, save=save)
+
+ def wikidata_populate(self, save=True):
+ Wikidata = type(self).Wikidata
+ client = Client()
+ # Probably should getlist
+ entity = client.get(self.wikidata)
+ for attname in dir(Wikidata):
+ if attname.startswith("_"):
+ continue
+ wd = getattr(Wikidata, attname)
+
+ self.wikidata_populate_attribute(client, entity, attname, wd, save=save)
+ if hasattr(Wikidata, '_supplement'):
+ for attname, wd in Wikidata._supplement(self):
+ self.wikidata_populate_attribute(client, entity, attname, wd, save=save)