Creating instances of Django proxy models from their base class - python

I have a series of models that look like this:
class Analysis(models.Model):
analysis_type = models.CharField(max_length=255)
def important_method(self):
...do stuff...
class SpecialAnalysis(Analysis):
class Meta:
proxy = True
def important_method(self):
...something different...
This is all pretty standard. However, what I'd like to do is automatically convert an Analysis model to a proxy model based on the value of the analysis_type field. For example, I'd like to be able to write code that looks like this:
>>> analysis = Analysis.objects.create(analysis_type="nothing_special")
>>> analysis.__class__
<class 'my_app.models.Analysis'>
>>> analysis = Analysis.objects.create(analysis_type="special")
>>> analysis.__class__
<class 'my_app.models.SpecialAnalysis'>
>>> analysis = Analysis.objects.get(pk=2)
>>> analysis.__class__
<class 'my_app.models.SpecialAnalysis'>
>>> # calls the ``important_method`` of the correct model
>>> for analysis in Analysis.objects.all():
... analysis.important_method()
Is this even remotely possible? A similar question was asked here, which actually gives some code for the iteration example, but it still leaves me with the question of how to get or create an instance of a proxy class from its parent. I suppose I could just override a bunch of manager methods, but I feel like there must be a more elegant way to do it.

I haven't found a "clean" or "elegant" way to do this. When I ran into this problem I solved it by cheating Python a little bit.
class Check(models.Model):
check_type = models.CharField(max_length=10, editable=False)
type = models.CharField(max_length=10, null=True, choices=TYPES)
method = models.CharField(max_length=25, choices=METHODS)
'More fields.'
def __init__(self, *args, **kwargs):
super(Check, self).__init__(*args, **kwargs)
if self.check_type:
map = {'TypeA': Check_A,
'TypeB': Check_B,
'TypeC': Check_C}
self.__class__ = map.get(self.check_type, Check)
def run(self):
'Do the normal stuff'
pass
class Check_A(Check):
class Meta:
proxy = True
def run(self):
'Do something different'
pass
class Check_B(Check):
class Meta:
proxy = True
def run(self):
'Do something different'
pass
class Check_C(Check):
class Meta:
proxy = True
def run(self):
'Do something different'
pass
It's not really clean but it was the easiest hack to find which solved my problem.
Maybe this is helps you, maybe it doesn't.
I'm also hoping someone else has a more pythonic solution to this problem since I'm counting the days till this method fails and comes back to haunt me..

This is a great approach and I don't particularly see it as a cheat. Here is IMHO some enhancements to the __init__ function so it doesn't has to change when you add more classes.
def __init__(self, *args, **kwargs):
super(Analysis, self).__init__(*args, **kwargs)
if not self.__type and type(self) == Analysis:
raise Exception("We should never create a base Analysis object. Please create a child proxy class instead.")
for _class in Analysis.__subclasses__():
if self.check_type == _class.__name__:
self.__class__ = _class
break
def save(self, **kwargs):
self.check_type = self.__class__.__name__
super(Analysis, self).save(**kwargs)
Hope this helps!

Related

How to mock relations on Django

I'm currently running into a problem, I can't figure out how to mock a relation.
Let's say I have some model called
class MyModel(models.Model):
name = models.CharField(max_length=10)
class RelatedModel(models.Model):
my_model = models.OneToOneField(MyModel, on_delete=models.CASCADE, related_name='related_model')
name = models.CharField(max_length=10
And I have some repository class like this:
class MyModelRepository:
#staticmethod
def get_related_model(my_model):
try:
return my_model.related_model
except MyModel.related_model.RelatedObjectDoesNotExist:
# do some other thing
When writing unit-tests for this how can I mock the related_model to raise this exception or return some arbitrary instance?
Here's a way to do it without unittest.mock:
class MockRelatedModel:
#property
def related_model(self):
raise MyModel.related_model.RelatedObjectDoesNotExist()
If you do want to use unittest.mock you can use PropertyMock. Note, this hasn't been tested. And frankly, I think it might be easier to create an instance of MyModel without the related_model set on it and call MyModelRepository.get_related_model.
with patch('path.to.MyModel', new_callable=PropertyMock) as mock_model:
mock_model.related_model.side_effect = MyModel.related_model.RelatedObjectDoesNotExist()
instance = MyModel()
MyModelRepository.get_related_model(instance)

How to override the model.Manager.create() method in Django?

I have plenty of Hardware models which have a HardwareType with various characteristics. Like so:
# models.py
from django.db import models
class HardwareType(model.Models):
name = models.CharField(max_length=32, unique=True)
# some characteristics of this particular piece of hardware
weight = models.DecimalField(max_digits=12, decimal_places=3)
# and more [...]
class Hardware(models.Model):
type = models.ForeignKey(HardwareType)
# some attributes
is_installed = models.BooleanField()
location_installed = models.TextField()
# and more [...]
If I wish to add a new Hardware object, I first have to retrieve the HardwareType every time, which is not very DRY:
tmp_hd_type = HardwareType.objects.get(name='NG35001')
new_hd = Hardware.objects.create(type=tmp_hd_type, is_installed=True, ...)
Therefore, I have tried to override the HardwareManager.create() method to automatically import the type when creating new Hardware like so:
# models.py
from django.db import models
class HardwareType(model.Models):
name = models.CharField(max_length=32, unique=True)
# some characteristics of this particular piece of hardware
weight = models.DecimalField(max_digits=12, decimal_places=3)
# and more [...]
class HardwareManager(models.Manager):
def create(self, *args, **kwargs):
if 'type' in kwargs and kwargs['type'] is str:
kwargs['type'] = HardwareType.objects.get(name=kwargs['type'])
super(HardwareManager, self).create(*args, **kwargs)
class Hardware(models.Model):
objects = HardwareManager()
type = models.ForeignKey(HardwareType)
# some attributes
is_installed = models.BooleanField()
location_installed = models.TextField()
# and more [...]
# so then I should be able to do:
new_hd = Hardware.objects.create(type='ND35001', is_installed=True, ...)
But I keep getting errors and really strange behaviors from the ORM (I don't have them right here, but I can post them if needed). I've searched in the Django documentation and the SO threads, but mostly I end up on solutions where:
the Hardware.save() method is overridden (should I get the HardwareType there ?) or,
the manager defines a new create_something method which calls self.create().
I also started digging into the code and saw that the Manager is some special kind of QuerySet but I don't know how to continue from there. I'd really like to replace the create method in place and I can't seem to manage this. What is preventing me from doing what I want to do?
The insight from Alasdair's answer helped a lot to catch both strings and unicode strings, but what was actually missing was a return statement before the call to super(HardwareManager, self).create(*args, **kwargs) in the HardwareManager.create() method.
The errors I was getting in my tests yesterday evening (being tired when coding is not a good idea :P) were ValueError: Cannot assign None: [...] does not allow null values. because the subsequent usage of new_hd that I had create()d was None because my create() method didn't have a return. What a stupid mistake !
Final corrected code:
class HardwareManager(models.Manager):
def create(self, *args, **kwargs):
if 'type' in kwargs and isinstance(kwargs['type'], basestring):
kwargs['type'] = HardwareType.objects.get(name=kwargs['type'])
return super(HardwareManager, self).create(*args, **kwargs)
Without seeing the traceback, I think the problem is on this line.
if 'type' in kwargs and kwargs['type'] is str:
This is checking whether kwargs['type'] is the same object as str, which will always be false.
In Python 3, to check whether `kwargs['type'] is a string, you should do:
if 'type' in kwargs and isinstance(kwargs['type'], str):
If you are using Python 2, you should use basestring, to catch byte strings and unicode strings.
if 'type' in kwargs and isinstance(kwargs['type'], basestring):
I was researching the same problem as you and decided not to use an override.
In my case making just another method made more sense given my constraints.
class HardwareManager(models.Manager):
def create_hardware(self, type):
_type = HardwareType.objects.get_or_create(name=type)
return self.create(type = _type ....)

Django model polymorphism, using proxy inheritance

This might sound like a duplicate, but I don't think it is.
I need to do something a bit similar to what the asker did there : django model polymorphism with proxy inheritance
My parent needs to implement a set of methods, let's call them MethodA(), MethodB(). These methods will never be used directly, they will always be called through child models (but no, abstract class is not the way to go for various reasons).
But this is where it becomes trickier :
Each child model inherits from a specific module (moduleA, moduleB), they all implement the same method names but do something different. The calls are made through the parent model, and are redirected to the childs depending on the values of a field
Since I guess it's not very clear, here is some pseudo-code to help you understand
from ModuleA import CustomClassA
from ModuleB import CustomClassB
class ParentModel(models.Model):
TYPE_CHOICES = (
('ChildModelA', 'A'),
('ChildModelB', 'B'),
)
#some fields
type = models.CharField(max_length=1, choices=TYPE_CHOICES)
def __init__(self, *args, **kwargs):
super(ParentModel, self).__init__(*args, **kwargs)
if self.type:
self.__class__ = getattr(sys.modules[__name__], self.type)
def MethodA():
some_method()
def MethodB():
some_other_method()
class ChildModelA(ParentModel, CustomClassA):
class Meta:
proxy = True
class ChildModelB(ParentModel, CustomClassB):
class Meta:
proxy = True
In ModuleA :
class CustomClassA():
def some_method():
#stuff
def some_other_method():
#other stuff
In ModuleB :
class CustomClassB():
def some_method():
#stuff
def some_other_method():
#other stuff
Right now, the problem is that the class change works, but it does not inherit from ChildModelA or B.
Is this even possible? If yes, how can I make it work, and if no, how could I do this elegantly, without too much repetition?
A proxy model must inherit from exactly one non-abstract model class. It seems that both CustomClass and ParentModel are non-abstract. I would suggest to make CustomClass abstract since no attributes are defined.
This is explained in dept here: https://docs.djangoproject.com/en/3.2/topics/db/models/#proxy-models

How to produce a TabularInline that spans models

I have a model (Booking) with a OneToOneField (Thread) that subsequently has a ForeignKey relationship (Message). I would like to show a list of messages on the Booking admin, but with the Thread model in between it appears that this is hard/not possible?
Class Booking(Model):
...
thread = models.OneToOneField('user_messages.Thread', verbose_name='thread')
class Thread(Model):
...
class Message(Model):
thread = models.ForeignKey(Thread, related_name="messages")
Is there a way I can set up my BookingAdmin with an inline that can display messages (spanning across the thread relationship)? Something like:
class MessageInline(TabularInline):
model = Message
fk_name = '???'
class BookingAdmin(ModelAdmin):
inlines = [MessageInline, ]
I'm happy to override the way the Inlines work if that's the best way, but I'm not sure where to tackle that. It looks like overriding *get_formset* might do the trick?
This isn't completely tested yet, but appears to work. The solution is to have an inline and formset with hooks to replace the booking with the attached thread...
class BookingMessageFormset(BaseInlineFormSet):
'''Given a Booking instance, divert to its Thread'''
def __init__(self, *args, **kwargs):
if 'instance' in kwargs:
kwargs['instance'] = kwargs['instance'].thread
else:
raise Exception() # TODO Not sure if/when this happens
BaseInlineFormSet.__init__(self, *args, **kwargs)
class MessageInline(admin.TabularInline):
model = Message
formset = BookingMessageFormset
def __init__(self, parent_model, admin_site):
'''Override parent_model'''
super(MessageInline, self).__init__(Thread, admin_site)

What's the easiest way of finding a child instance from a parent instance in Django?

My application uses class inheritance to minimize repetition across my models. My models.py looks kind of like this:
class BaseModel(models.Model):
title = models.CharField(max_length=100)
pub_date = models.DateField()
class Child(BaseModel):
foo = models.CharField(max_length=20)
class SecondChild(BaseModel):
bar = models.CharField(max_length=20)
Now most of the time, my views and templates only deal with instances of Child or SecondChild. Once in a while, however, I have a situation where I have an instance of BaseModel, and need to figure out which class is inheriting from that instance.
Given an instance of BaseModel, let's call it base, Django's ORM offers base.child and base.secondchild. Currently, I have a method that loops through all of them to figure it out. It would look something like this:
class BaseModel(models.Model):
...
def get_absolute_url(self):
url = None
try:
self.child
url = self.child.get_absolute_url()
except Child.DoesNotExist:
pass
if not url:
try:
self.secondchild
url = self.secondchild.get_absolute_url()
except SecondChild.DoesNotExist:
pass
if not url:
url = '/base/%i' % self.id
return url
That is hopelessly ugly, and gets uglier with every additional child class I have. Does anybody have any ideas on a better, more pythonic way to go about this?
Various forms of this question pop up here regularly. This answer demonstrates a generic way to "cast" a parent type to its proper subtype without having to query every subtype table. That way you wouldn't need to define a monster get_absolute_url on the parent which covers all the cases, you'd just convert to the child type and call get_absolute_url normally.
I haven't messed with Django inheitance much, so I suppose you can't override get_absolute_url() in the model classes?
Perhaps the visitor pattern could help if there are lot of functions that need this in many different places.
I haven't tested this, but it might be worth tinkering with:
def get_absolute_url(self):
subclasses = ('child', 'secondchild', )
for subclass in subclasses:
if hasattr(self, subclass):
return getattr(self, subclass).get_absolute_url()
return '/base/%i' % self.id

Categories

Resources