Skip to content

Instantly share code, notes, and snippets.

View danilovmy's full-sized avatar

Maxim Danilov danilovmy

View GitHub Profile
@danilovmy
danilovmy / models.py
Last active March 31, 2022 08:44
Products model with o2m relation to Image model.
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)
@danilovmy
danilovmy / admin.py
Created March 31, 2022 08:47
ModelAdmins for Product and Image models.
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')
@danilovmy
danilovmy / admin.py
Last active March 31, 2022 08:52
Added inline in ProductModel Admin
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):
@danilovmy
danilovmy / admin.py
Created March 31, 2022 09:01
Create custom field in ProductModelAdmin and render first Inline in this field.
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)
@danilovmy
danilovmy / admin.py
Created March 31, 2022 09:07
Move custom field in the middle of AdminForm
@admin.register(Product)
class ProductModelAdmin(admin.ModelAdmin):
inlines = ImageAdminInline,
fields = 'title','image_inline', 'price'
readonly_fields= 'image_inline', # method as readonly field
@danilovmy
danilovmy / admin.py
Created March 31, 2022 09:11
Sorry, ModelAdmin is singleton.
@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)
@danilovmy
danilovmy / admin.py
Last active April 21, 2022 13:05
Check if ModelAdmin.instance is singleton with a special method.
@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'
@danilovmy
danilovmy / time_counter_middleware.py
Created May 2, 2022 18:57
Django middleware, to measure the view work time
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)
@danilovmy
danilovmy / django.contrib.admin.sites.py
Created May 2, 2022 19:30
important moment from django.contrib.admin.sites.py to solve ModelAdmin singleton problem
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
@danilovmy
danilovmy / django.contrib.admin.options.py
Created May 2, 2022 19:37
ModelAdmin.get_urls method. Here is the singleton problem.
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