Admin generic inlines for multi-table subclassed models broken --- any alternatives?

Here's what I'm trying to do, and failing...

I have a File model which has a generic-relation to other objects:

class File(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()
    file = models.FileField(upload_to='files/%Y/%m/%d')
    # etc....

I also want to have a sub-class of File to deal with the specific case of images to be displayed in-page, rather than downloaded:

class Image(File):
    file = models.ImageField(upload_to='files/%Y/%m/%d')

All of the above works fine, including generic inlines of the File model, until I want to use a generic-inline of the Image model --- the save process fails to create the base class instance and so raises an error stating that the Image.file_ptr (the 'secret' foreign key to the base class) cannot be None.

So, basically, generic inlines do not properly support multi-table inheritance at the moment.

It's quite likely that I'm making this more complicated than it need be, so can anyone suggest either a fix for this problem, or a better way of achieving the same end?

Please let me know if you need further clarification.

Asked by: Arthur846 | Posted: 01-10-2021

Answer 1

Inheritance can be implemented two ways in a relational model.

A subclass can be a new table with all the same columns as the superclass repeated. This works well when you have an abstract superclass or subclass features that override the superclass.

A subclass can be just the unique columns with a join to the superclass table. This works well when you have a concrete superclass.

In your case, it looks you might have the following.

class FileFacts( models.Model ):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()

class DownloadFile( models.Model ):
    facts = models.ForeignKey( FileFacts )
    file = models.FileField(upload_to='files/%Y/%m/%d')

class InlineImage( models.Model ):
    facts = models.ForeignKey( FileFacts )
    file = models.ImageField(upload_to='files/%Y/%m/%d')

This is my preference for handling a subclass-like model.

Answered by: Brad472 | Posted: 02-11-2021

Similar questions

Memory Efficient Alternatives to Python Dictionaries

In one of my current side projects, I am scanning through some text looking at the frequency of word triplets. In my first go at it, I used the default dictionary three levels deep. In other words, topDict[word1][word2][word3] returns the number of times these words appear in the text, topDict[word1][word2] returns a dictionary with all the words that appeared following words 1 and 2, etc. ...

deployment - Are there any other good alternatives to zc.buildout and/or virtualenv for installing non-python dependencies?

I am a member of a team that is about to launch a beta of a python (Django specifically) based web site and accompanying suite of backend tools. The team itself has doubled in size from 2 to 4 over the past few weeks and we expect continued growth for the next couple of months at least. One issue that has started to plague us is getting everyone up to speed in terms of getting their development environment configured and...

coding style - Alternatives for returning multiple values from a Python function

Closed. This question is opinion-based. It is not c...

Any alternatives to IronPython, Python for .NET for accessing CLR from python?

Are there any alternatives to Python for .NET or IronPython for accessing .NET CLR? Both of these seem to have downsides in that Python for .NET is not under active development (as far as I can tell) and you lose some features available in CPython if you use IronPython. So are there any alternatives?

python - Alternatives to using pack_into() when manipulating a list of bytes?

I'm reading in a binary file into a list and parsing the binary data. I'm using unpack() to extract certain parts of the data as primitive data types, and I want to edit that data and insert it back into the original list of bytes. Using pack_into() would make it easy, except that I'm using Python 2.4, and pack_into() wasn't introduced until...

Python: Alternatives to pickling a module

I am working on my program, GarlicSim, in which a user creates a simulation, then he is able to manipulate it as he desires, and then he can save it to file. I recently tried implementing the saving feature. The natural thing that occured to me is to pickle the Project object, which contains the entire simulation. Problem is, the

python - How does Google Books work? Are there any open source alternatives?

I have been asked to publish a complete book online similar way Google Books does? i.e. it's viewable and printable but not download-able. Is the process is basically "high quality scanning"? are there any open source solution to "mass generation" of "watermark" on those high quality images. Suppose you have an original image. and when the user views it online, I re-create the image add watermark and some other te...

python - Are there any alternatives to py2exe?

Closed. This question needs to be more focused. It ...

python - Alternatives to FontForge

I use python bindings for FontForge under Ubuntu. It constantly runs into crash without any clues about the reason, e.g. segmentation fault, memory mapping errors, etc. All what I need is to read font file's (.ttf and .otf) meta data (font name, family name, version, unique id, copyright, license, designer, designer url, etc) and count the glyphs it has. Are there any alternatives to fontforge which do abov...

Are there any free alternatives to the Ranorex library (Python, Windows)?

I am interested in the Python one. I wish to automate some GUI under Windows. What is the best open source library for that with no strings attached? Thanks.

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python