Reputation: 4968
I am attempting to model study resources (books, DVDs, etc) and what prerequisites those resources have.
A prerequisite is a resource also. So my question is, what Django modelling technique best captures this relation? The study resource is straightforward:
from django.db import models
from django.utils import timezone
class Resource(models.Model):
title = models.CharField(max_length=300)
shortcode = models.CharField(max_length=20, null=True, blank=True)
img = models.URLField(null=True, blank=True)
summary = models.TextField(null=True, blank=True)
url = models.URLField('Link to Resource', null=True, blank=True)
pub_date = models.DateTimeField('date published')
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
def __unicode__(self):
return self.title
However, I am not sure what the "best" way to model the
prerequisite, given that it is a Resource
also and most importantly that there are a variable number of prequisites (thus eliminating the possibility of simply having a self-referential parent ). Here is my current approach:
class Prereq(models.Model):
prereq = models.ForeignKey(Resource, null=True, related_name='prereq_backlink')
resource = models.ForeignKey(Resource, null=True, related_name='prereq_resource')
def __unicode__(self):
return self.resource.title
The problem with this approach is that accessing a prerequisite does not give me a full resource object to work with, so I cannot access its various fields (title, shortcode, etc):
schemelab@schemelab2:~/domains/org/metaperl/tmp/idhhb/django/mysite$ ./manage.py shell
>>> from idhhb.models import Resource, Prereq
from idhhb.models import Resource, Prereq
>>> p = Resource.objects.get(id=2)
>>> p.title
u'American Book of the Dead'
>>> r = Resource.objects.get(id=2)
r = Resource.objects.get(id=2)
>>> p1 = r.prereq_backlink
>>> p2 = r.prereq_resource
>>> p1
<django.db.models.fields.related.RelatedManager object at 0x2e279d0>
>>> p2
<django.db.models.fields.related.RelatedManager object at 0x2e27a90>
>>> p1.title
p1.title
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'RelatedManager' object has no attribute 'title'
>>> p2.title
p2.title
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'RelatedManager' object has no attribute 'title'
>>>
Upvotes: 0
Views: 89
Reputation: 4968
Thanks to this blog post I was able to implement this using ManyToManyField
as suggested. I must say this blog post is better than any docs I found on the Django site itself.
MODEL:
class Resource(models.Model):
title = models.CharField(max_length=300)
shortcode = models.CharField(max_length=20, null=True, blank=True)
img = models.URLField(null=True, blank=True)
summary = models.TextField(null=True, blank=True)
url = models.URLField('Link to Resource', null=True, blank=True)
pub_date = models.DateTimeField('date published')
prereqs = models.ManyToManyField(
'self', through='Prereq', symmetrical=False, related_name='prerequired')
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
def __unicode__(self):
return self.title
class Prereq(models.Model):
resource = models.ForeignKey(Resource, null=True, related_name='main_resource')
prereq = models.ForeignKey(Resource, null=True, related_name='prereq_resource')
ADMIN:
from django.contrib import admin
from idhhb.models import Resource, Prereq
class PrereqInline(admin.TabularInline):
model = Prereq
fk_name = 'resource'
extra = 5
class ResourceAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': 'title shortcode img summary url pub_date'.split() }),
]
inlines = [PrereqInline,]
admin.site.register(Resource, ResourceAdmin)
TEMPLATE:
{% for prereq in resource.prereqs.all %}
<li>{{ prereq.id }}</li>
{% endfor %}
Upvotes: 1
Reputation: 45575
prereq_backlink
and prereq_resource
are managers so access to the Resource
will look like:
r = Resource.objects.get(id=2)
for prereq in r.prereq_backlink.all():
print prereq.resource.title
But I join to @daniel-roseman's question: why you didn't use the ManyToManyField
for this task?
class Resource(models.Model):
...
prereqs = models.ManyToManyField('self')
Upvotes: 0