views:

31

answers:

1

Hello,

i'm building a simple app for Django and I'm facing some problems with my models design, and the use of 'inlines' in the administration interface.

The app is to manage projects.
A project consists of a name, a description, other fields (e.g. tags...) and of multiple urls (like project url, source code repository url) but the number is not fixed, they can have 1, 2 or more urls (i think 0 will never be the case).

I have first created simple models like:

class Url(models.Model):
    name = models.CharField(max_length=100)
    url = models.URLField()


class Project(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=300)
      ...
    urls = models.ManyToManyField(Url, blank=True)

but with these models I did't manage to have an admin interface where i can had a project and add at the same time one or more urls.
I have tried to use 'inlines' in the admin website like indicated in the doc but without success.

I'm not even sure the models/database design (e.g. urls won't be reused in various projects, and the manytomanyfield let you choose between allready existing urls which may not be necessary), but I don't know what can be other solutions (lists, ...).

Can anyone help me with this (simple i think) problem?
Indicate me some useful guidelines to choose a models/db design?
Even maybe point me to some example code implementing this sort of problem, with admin interface as indicated?

Thanks for responses and don't hesitate to ask details if it's not clear enough.

+1  A: 

I think I have found the beginning of a solution to my problem.
Changing the ManyToManyField in Project to a ForeignKey in Url, and creating inlines like indicated below

# models.py
class Project(models.Model):
  name = models.CharField(max_length=100)
  description = models.CharField(max_length=300)
  ...

class Url(models.Model):
  name = models.CharField(max_length=100)
  url = models.URLField()
  project = models.ForeignKey(Project)

# admin.py
class UrlInline(admin.TabularInline):
  model = Url

class ProjectAdmin(admin.ModelAdmin):
  inlines = [ UrlInline, ]

admin.site.register(Project,ProjectAdmin)

Now i can add urls specific to a project, edit them, even delete them :)
And I don't see urls used in other projects.

I think it does the job to begin, and, for information, I found ideas on the right sidebar looking at this other post http://stackoverflow.com/questions/702637/django-admin-inline-inlines-or-three-model-editing-at-once :)

xkz
+1 I was about to say the very same. I think this is the way you should do it.
Felix Kling
Yes, it seems to work well. I only added this function to the Project model to ease urls access from templates: def urls(self): return self.url_set.filter(project=self)
xkz
It seems that i don't even need to create a 'urls' function. In my templates i can access to project.url_set.all
xkz