This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.db import models | |
| from django.utils.translation import gettext_lazy as _ | |
| class Product(models.Model): | |
| title = models.CharField(verbose_name=_('Title of product'), max_length=255) | |
| price = models.DecimalField(verbose_name=_('Price of product'), max_digits=6, decimal_places=2) | |
| class Image(models.Model): | |
| src = models.ImageField(verbose_name=_('Imagefile')) | |
| product = models.ForeignKey(Product, verbose_name=_('Link to product'), on_delete=models.CASCADE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.contrib import admin | |
| from .models import Image, Product | |
| @admin.register(Product) | |
| class ProductModelAdmin(admin.ModelAdmin): | |
| fields = ('title', 'price') | |
| @admin.register(Image) | |
| class ImageModelAdmin(admin.ModelAdmin): | |
| fields = ('src', 'product') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.contrib import admin | |
| from django.contrib.admin.options import TabularInline | |
| from .models import Image, Product | |
| class ImageAdminInline(TabularInline): | |
| extra = 1 | |
| model = Image | |
| @admin.register(Product) | |
| class ProductModelAdmin(admin.ModelAdmin): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.contrib import admin | |
| from django.contrib.admin.options import TabularInline | |
| from django.template.loader import get_template | |
| from .models import Image, Product | |
| class ImageAdminInline(TabularInline): | |
| extra = 1 | |
| model = Image | |
| @admin.register(Product) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @admin.register(Product) | |
| class ProductModelAdmin(admin.ModelAdmin): | |
| inlines = ImageAdminInline, | |
| fields = 'title','image_inline', 'price' | |
| readonly_fields= 'image_inline', # method as readonly field |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @admin.register(Product) | |
| class ProductModelAdmin(admin.ModelAdmin): | |
| inlines = (ImageAdminInline,) | |
| fields = ('title', 'image_inline', 'price') | |
| readonly_fields= ('image_inline',) # we set the method as readonly field | |
| def image_inline(self, obj=None, *args, **kwargs): | |
| context = obj.response['context_data'] | |
| inline = context['inline_admin_formset'] = context['inline_admin_formsets'].pop(0) | |
| return get_template(inline.opts.template).render(context, obj.request) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @admin.register(Product) | |
| class ProductModelAdmin(admin.ModelAdmin): | |
| fields = ('title', 'image_inline', 'price') | |
| readonly_fields= ('image_inline',) # we set the method as readonly field | |
| def image_inline(self, obj=None, *args, **kwargs): | |
| if obj.pk == 2: | |
| self.hello = 'this is the second obj' | |
| else: | |
| self.hello = 'hello this is the first obj' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from datetime import datetime | |
| class TimerMiddleware: | |
| def __init__(self, get_response): | |
| self.get_response = get_response | |
| def __call__(self, request): | |
| start_time = datetime.now() | |
| response = self.get_response(request) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_urls(self): # line number 245 | |
| ... | |
| # many spagetty code | |
| ... | |
| # line number 281 | |
| for model, model_admin in self._registry.items(): | |
| urlpatterns += [path('%s/%s/' % (model._meta.app_label, model._meta.model_name), include(model_admin.urls))] | |
| # please see all code on https://github.com/django/django/blob/main/django/contrib/admin/sites.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_urls(self): # row 617 | |
| from django.urls import path | |
| def wrap(view): | |
| def wrapper(*args, **kwargs): | |
| return self.admin_site.admin_view(view)(*args, **kwargs) | |
| wrapper.model_admin = self | |
| return update_wrapper(wrapper, view) | |
| info = self.model._meta.app_label, self.model._meta.model_name |
OlderNewer