Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is an <code>InheritanceManager</code> provided as part of <code>django-model-utils</code> which allows you to do this, see <a href="https://django-model-utils.readthedocs.org/en/latest/index.html" rel="nofollow noreferrer">docs</a>.</p> <p>To install in Linux / Mac:</p> <pre><code>sudo pip install django-model-utils </code></pre> <p>Annoyingly, installing using <code>easy_install</code> or <code>pip</code> on windows is not quite as straight forward, see: <a href="https://stackoverflow.com/questions/1449494/how-do-i-install-python-packages-on-windows">How do I install Python packages on Windows?</a>. A quick and dirty method is to download the <code>django-model-util/</code> directory from <a href="https://bitbucket.org/carljm/django-model-utils/src" rel="nofollow noreferrer">here</a> into the top directory of your django project, this is handy if you intend to copy the entire project across for deployment to a production webserver.</p> <p>In order to use the <code>InheritanceManager</code>, the models need to be refactored slightly:</p> <pre><code>from django.db import models from django.contrib.auth.models import User from datetime import datetime from model_utils.managers import InheritanceManager get_upload_file_name = 'images/' # I added this to debug models class BaseImage(models.Model): user = models.ForeignKey(User) image = models.ImageField(upload_to=get_upload_file_name) caption = models.CharField(max_length=200) pub_date = models.DateTimeField(default=datetime.now) objects = InheritanceManager() class BackgroundImage(BaseImage): pass class ProfilePicture(BaseImage): pass class Album(models.Model): user = models.ForeignKey(User) name = models.CharField(max_length=200) pub_date = models.DateTimeField(default=datetime.now) class Meta: ordering = ['-pub_date'] verbose_name_plural = ('Albums') def __unicode__(self): return self.name class Photo(BaseImage): album = models.ForeignKey(Album, default=3) </code></pre> <p>All the Image models now inherit from a common super class which creates an instance of the <code>InheritanceManager</code>. I've also moved up all the duplicated attributes into the superclass, but this isn't strictly necessary, using <code>InheritanceManager</code> means that any attributes which are not present in <code>BaseImage</code> can still be accessed in the template.</p> <p>To retrieve a list ordered by <code>-pubdate</code>:</p> <pre><code>BaseImage.objects.select_subclasses().order_by("-pub_date") </code></pre> <p>To use in a view:</p> <pre><code>def recentImages(request): r = BaseImage.objects.select_subclasses().order_by("-pub_date")[:20] return render_to_response("recentImages.html", { "imageList" : r }) </code></pre> <p>To use in a template:</p> <pre><code>{% for photo in imageList %} &lt;img src="{{ photo.image.url }}" /&gt; {% endfor %} </code></pre> <p>Is this something like what you are looking for?</p> <p><b>Edit</b></p> <p>The following code will still work fine, with the new models:</p> <pre><code>class UserProfile(models.Model): user = models.OneToOneField(User) permanent_address = models.TextField() temporary_address = models.TextField() profile_pic = models.ForeignKey(ProfilePicture) background_pic = models.ForeignKey(BackgroundImage) </code></pre> <p>Just make sure the names of the last two models in the <code>ForeignKey</code> relationship are correct!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload