Example¶
This app comes with a testproject
django project to run tests, and an
example
app to showcase how to use the jugemaj
app. We’ll show here
some parts extracted from the example
app.
So let’s start by creating one model. Let’s say we want a list of cats from wikidata.
We need to define the model:
class WikiDataModel(models.Model):
"""A django model to represent something available on wikidata."""
name = models.CharField(max_length=50)
wikidata = models.PositiveIntegerField()
And to populate it:
req = requests.get('https://query.wikidata.org/sparql', params={'format': 'json', 'query': QUERY_CATS})
for result in req.json()['results']['bindings']:
WikiDataModel.objects.create(name=result['itemLabel']['value'],
wikidata=wikidata_url_to_id(result['item']['value']))
Next, we can create an Election instance
admin = User.objects.create(username='example_admin')
election = Election.objects.create(name='Cats', creator=admin, end=now() + timedelta(days=365))
And declare our cats as candidates for it:
for cat in WikiDataModel.objects.all():
# In a migration, we need content_type & object_id.
# But in usual code, this would be:
# Candidate.objects.create(election=election, object=cat)
Candidate.objects.create(election=election, **content_type_and_object_id(cat))