On 11/19/2010 8:51 PM, Micah Carrick wrote:
> I'm having trouble coming up with a pretty solution to a seemingly
> simple task. I'm relatively new to Django.
> 
> I want to allow the end user to control various lists of links on the
> site used for navigation. The admin should allow the creation of "link
> groups" which have a collection of "links". These links would reference
> a pre-determined list of models. Let's say, for example, that there is a
> "link group" created for the footer links of a website. In the admin
> section, a user could add a link to a specific blog (from the Blog
> model), another link to the about us page (flatpages), etc.
> 
> In other words, I'm trying to associate individual records from a number
> of tables together as a group of objects having a URL. I'm trying to
> find a nice, abstract solution. Obviously I don't want actual URLs in
> the database. This is something I would use frequently so I want to see
> if I can find or write an app to do this--if I can come up with an
> elegant solution.
> 
> This would be nice, but, I can't imagine how it could be possible:
> 
> 
> class LinkGroup(models.Model):
>     site = models.ForeignKey(Site)
>     name = models.CharField()
> 
> class Links(models.Model):
>     link_group = ForeignKey(LinkGroup)
>     model_type = ???
>     model_id = ForeignKey(<to the PK of the above the model_type>) # no
> can do!
>     sort_order = PositiveIntegerField(default=100)
> 
> 
> This is an idea, however, I don't like having to reference the import in
> the DB. It's just begging for problems.
> 
> 
> class LinkModel(models.Model):
>     name = models.CharField() # such as "Flat Page"
>     model = models.CharField() # such as "myapp.models.FlatPage"
> 
> class LinkGroup(models.Model):
>     site = models.ForeignKey(Site)
>     name = models.CharField() # such as "Navigation Links"
> 
> class Link(models.Model):
>     text = CharField() # such as "About Us"
>     link_group = ForeignKey(LinkGroup)
>     model = ForeignKey(LinkModel)
>     model_id = PositiveIntegerField() # such as the PK for the
> myapp.models.FlatPage model
>     sort_order = PositiveIntegerField(default=100)
> 
> 
> Any suggestions?
> 
What's wrong with the first approach? I use a similar technique to put
pages in sections at http://holdenweb.com/ - here are the relevant models:

class Section(models.Model):
    secid = models.IntegerField(primary_key=True) # This field type is
corrected
    sectitle = models.CharField(max_length=50)
    secpath = models.CharField(max_length=50)
    secbgcolor = models.CharField(max_length=50)
    secstcolor = models.CharField(max_length=50)
    secpos = models.CharField(max_length=1)
    secsequence = models.IntegerField() # This field type is corrected
    sechandler = models.CharField(max_length=50)
    sechomeslot = models.IntegerField() # This field type is corrected
    sechometitle = models.CharField(max_length=50)
    secnews = models.CharField(max_length=20, null=True)
    class Meta:
        db_table = 'section'
        unique_together = (('secpath', ), ('sectitle', ))
    class Admin:
        list_display = ('sectitle', )
    def __str__(self):
        return self.sectitle

class Page(models.Model):
    pagpath = models.CharField(primary_key=True, max_length=50)
    pagdoctitle = models.CharField(max_length=250)
    pagtitle = models.CharField(max_length=200, blank=True, null=True)
    pagsecid = models.ForeignKey(Section, blank=True, null=True,
db_column='pagsecid') # This field
type is corrected
    pagsequence = models.IntegerField() # This field type is corrected
    pagtplname = models.CharField(max_length=50)
    paggenerated = models.SmallIntegerField()
    paglinkpath = models.CharField(max_length=50, blank=True, null=True)
    pagcontent = models.TextField()
    pagnavbarstuff = models.TextField(null=True, blank=True)
    pagctype = models.CharField(max_length=1, default="R") # This field
type is corrected
    pagid = models.IntegerField() # Note this should eventually become
the PK
    pagnews = models.CharField(max_length=20, blank=True, null=True)
    class Meta:
        db_table = 'page'
    def __str__(self):
        return self.pagpath

What is it that the first model didn't do for you?

regards
 Steve
-- 
DjangoCon US 2010 September 7-9 http://djangocon.us/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to