Login

Default to current/all sites in admin (updated!)

Author:
dokterbob
Posted:
October 12, 2008
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

This code sets the default sites for a sites ManyToMany property to Site.objects.all(), which makes sure you don't have to bother setting it for each item on a site.

This could easily be changed to Site.objects.get_current() to use the current site as default.

1
2
3
4
5
def get_default_sites():
    return [site.id for site in Site.objects.all()]

class MyModel(models.Model):
    sites = models.ManyToManyField(Site, default=get_default_sites)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.