Différences entre versions de « Django »
Sauter à la navigation
Sauter à la recherche
imported>SylvainBeucler m |
imported>SylvainBeucler m (exemples) |
||
Ligne 4 : | Ligne 4 : | ||
* [http://lincolnloop.com/blog/2009/jun/22/customizing-django-admin-eurodjangocon-2009/ Customizing the Django Admin at EuroDjangoCon 2009] | * [http://lincolnloop.com/blog/2009/jun/22/customizing-django-admin-eurodjangocon-2009/ Customizing the Django Admin at EuroDjangoCon 2009] | ||
* [http://lincolnloop.com/assets/Customizing_the_Django_Admin-EuroDjangoCon09.pdf Customizing_the_Django_Admin-EuroDjangoCon09.pdf] | * [http://lincolnloop.com/assets/Customizing_the_Django_Admin-EuroDjangoCon09.pdf Customizing_the_Django_Admin-EuroDjangoCon09.pdf] | ||
+ | |||
+ | Modifier le queryset dynamiquement: | ||
+ | <pre> | ||
+ | class StuffAdmin(admin.ModelAdmin) | ||
+ | ... | ||
+ | def queryset(self, request): | ||
+ | qs = super(self.__class__, self).queryset(request) | ||
+ | qs = qs.filter(user=request.user) | ||
+ | return qs | ||
+ | |||
+ | # or | ||
+ | def queryset(self, request): | ||
+ | qs = self.model._default_manager.filter(user=request.user) | ||
+ | return qs | ||
+ | </pre> | ||
+ | |||
+ | Forcer des champs dynamiquement: | ||
+ | <pre> | ||
+ | class StuffAdmin(admin.ModelAdmin) | ||
+ | ... | ||
+ | def save_model(self, request, object, form, change): | ||
+ | object.user = request.user | ||
+ | object.save() | ||
+ | </pre> | ||
== Liens == | == Liens == | ||
* [http://www.mercurytide.co.uk/news/article/django-cheat-sheet/ Fiche de triche Django] | * [http://www.mercurytide.co.uk/news/article/django-cheat-sheet/ Fiche de triche Django] |
Version du 27 février 2010 à 17:52
Personnaliser l'interface d'admin
De quoi aller plus loin, principalement les quelques derniers slides (ce sont des choses que j'avais cherché pendant trois plombes):
Modifier le queryset dynamiquement:
class StuffAdmin(admin.ModelAdmin) ... def queryset(self, request): qs = super(self.__class__, self).queryset(request) qs = qs.filter(user=request.user) return qs # or def queryset(self, request): qs = self.model._default_manager.filter(user=request.user) return qs
Forcer des champs dynamiquement:
class StuffAdmin(admin.ModelAdmin) ... def save_model(self, request, object, form, change): object.user = request.user object.save()