Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8414967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:18:31+00:00 2026-06-10T01:18:31+00:00

I have some models like that: class BaseModel(models.Model): created_by = models.ForeignKey(User, related_name=%(app_label)s_%(class)s_created) created_date =

  • 0

I have some models like that:

class BaseModel(models.Model):
    created_by = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_created")
    created_date = models.DateTimeField(_('Added date'), auto_now_add=True)
    last_updated_by = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_updated")
    last_updated_date = models.DateTimeField(_('Last update date'), auto_now=True)

    class Meta:
        abstract = True

class Image(BaseModel):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    name = models.CharField(_('Item name'), max_length=200, blank=True)
    image = models.ImageField(_('Image'), upload_to=get_upload_path)

    def save(self, *args, **kwargs):
        if self.image and not GALLERY_ORIGINAL_IMAGESIZE == 0:
            width, height = GALLERY_ORIGINAL_IMAGESIZE.split('x')
            super(Image, self).save(*args, **kwargs)

            filename = os.path.join( settings.MEDIA_ROOT, self.image.name )
            image = PILImage.open(filename)

            image.thumbnail((int(width), int(height)), PILImage.ANTIALIAS)
            image.save(filename)

        super(Image, self).save(*args, **kwargs)

class Album(BaseModel):
    name = models.CharField(_('Album Name'), max_length=200)
    description = models.TextField(_('Description'), blank=True)
    slug = models.SlugField(_('Slug'), max_length=200, blank=True)
    status = models.SmallIntegerField(_('Status'),choices=ALBUM_STATUSES)

    images = generic.GenericRelation(Image)

I use BaseModel abstract model for my all models to track save and update logs. I can use ModelAdmin class to set user fields automatically:

class BaseAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user

        obj.last_updated_by = request.user
        obj.save()


class AlbumAdmin(BaseAdmin):
    prepopulated_fields = {"slug": ("name",)}
    list_display = ('id','name')
    ordering = ('id',)

That works. All BaseAdmin fields are filled automatically. But I want to add Images to Albums by Inline. So, I change my admin.py like that:

from django.contrib.contenttypes import generic

class ImageInline(generic.GenericTabularInline):
    model = Image
    extra = 1

class AlbumAdmin(BaseAdmin):
    prepopulated_fields = {"slug": ("name",)}
    list_display = ('id','name')
    ordering = ('id',)

    inlines = [ImageInline,]

When I save page, I get an error: gallery_image.created_by_id may not be NULL on first super(Image, self).save(*args, **kwargs) row of Image model save method. I know it’s because of GenericTabularInline class doesn’t have a “save_model” method to override.

So, the question is, how can I override save method and set current user on InlineModelAdmin classes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:18:32+00:00Added an answer on June 10, 2026 at 1:18 am

    I have found a solution on another question: https://stackoverflow.com/a/3569038/198062

    So, I changed my BaseAdmin model class like that, and it worked like a charm:

    from models import BaseModel
    
    class BaseAdmin(admin.ModelAdmin):
        def save_model(self, request, obj, form, change):
            if not change:
                obj.created_by = request.user
    
            obj.last_updated_by = request.user
            obj.save()
    
        def save_formset(self, request, form, formset, change):
            instances = formset.save(commit=False)
    
            for instance in instances:
                if isinstance(instance, BaseModel): #Check if it is the correct type of inline
                    if not instance.created_by_id:
                        instance.created_by = request.user
    
                    instance.last_updated_by = request.user            
                    instance.save()
    

    Note that, you must extend same abstract class for the ModelAdmin that contains the inlines to use this solution. Or you can add that save_formset method to ModelAdmin that contains the inline specifically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some models that look like this: class UserProfile(models.Model): user = models.OneToOneField(User) favorite_books
I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
I've got some models set up like this: class AppGroup(models.Model): users = models.ManyToManyField(User) class
So I have some Django 1.3 models like this: class Type(models.Model): is_bulk = models.BooleanField()
I have some simple Django Models like this: class Event(models.Model): # some stuff class
I wanted to write some code like this: class SomeModel(models.Model): field = models.ForeignKey(SomeOtherModel) def
Let's say i have many models like that: class ExampleModel(models.Model): photo = models.ImageField(upload_to='photos/') image
I have a models A and B , that are like this: class A(models.Model):
I have some nested models that look something like: class Company has_many :managers end
Say I have some django models, something like this: class Address(models.Model): pass class Person(models.Model):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.