Luís de Sousa
Luís de Sousa

Reputation: 6841

Adding fields to the Site model

I have an application that should serve various sites. The Sites framework is already there for that, but in this application some sites share certain assets (CSS, etc). Thus I would like to have an additional model (say Asset) with a One-to-Many relationship with Site.

I believe this essentially boils down to add a custom field to the sites.models.Site model. Is it possible? How?

Upvotes: 0

Views: 408

Answers (1)

catavaran
catavaran

Reputation: 45575

When you create a foreign key to the Site model it will be available in the site instance automatically:

site.asset_set.all()

You can even provide the related_name to the backward relation for more readability:

class Asset(models.Model):
    site = models.ForeignKey(Site, related_name='assets')

...

site.assets.all()

Upvotes: 2

Related Questions