because you class no #Configuration,
please add #Configuration on class
#Configuration
public class HotelAssistCancelListener implements InitializingBean
Related
I have a Place model with subclasses Restaurant and Bar. I attached InheritanceManager from django-model-utils to Place to use the select_subclasses() method to get instances of the subclass.
from model_utils.managers import InheritanceManager
class Place(models.Model):
# ...
objects = InheritanceManager()
class Restaurant(Place):
# ...
class Bar(Place):
# ...
Everything worked fine. But now I want to set the order of the model Place with django-ordered-model. This package also uses a manager:
...
objects = OrderedModelManager()
...
How to combine them?
class BaseModel(models.Model): # base class should subclass 'django.db.models.Model'
creation_date = models.DateTimeField(..) # define the common field1
class Meta:
abstract=True # Set this model as Abstract
Inherit this Base class in models
After creating the abstract base class BaseModel, I inherited this class in my models.
class MyModel1(BaseModel): # inherit the base model class
# define other non-common fields here
...
After creating an object of class 'MyModel1', I want the 'creation_date' field to be shown in admin interface.
So that I can see the datetime when an object of class 'MyModel1' is created.
Solution:
class MyModel1Admin(admin.ModelAdmin):
readonly_fields= ['creation_date',]
admin.site.register(MyModel1,MyModel1Admin)
Let's say I have a parent class (Products) and child class related by OneToOneField. Now let's say I have a Products instance object. How will I be able to access all the fields of child ?
class Products(models.Model):
......
class Child1(models.Model):
parent=models.OneToOneField(Products)
......
class Child2(models.Model):
parent=models.OneToOneField(Products)
......
Now let
product_instance=Products.objects.get(id=id)
How can I access child fields without knowing the child class name?
There is package called DeepCollector using this you can get all related objects
from deep_collector.core import DeepCollector
collector = DeepCollector()
collector.collect(Products)
related_objects = collector.get_collected_objects()
try this...
I have 2 classes defined like:
class Parent(models.Model)
# class definition
And the second class:
class Child(models.Manager):
def get_queryset(self):
pass
Now I want to override the 'objects' of class Parent. Normally it will go like this:
class Parent():
objects = Child()
But I can't modify class Parent because it's a third party library.
Is there any workaround for this problem?
setattr(Parent, 'objects', Child())
write this line in end of file where you have your manager class.
this will override the objects attribute of Parent Class if exist or add objects attribute.
Example:
from django.db import models
class abc(models.Model):
pass
class pqr(models.Manager):
def get_queryset(self, *args, **kwargs):
print('in get_queryset')
setattr(abc, 'objects', pqr())
now in shell get or create object of parent class in this example abc class and perform any operation on in .
a = abc()
a.objects.all()
but this method is wrong it will override the objects attribute and if you want to use this method then use attribute name different than objects
I'm trying to inherit from a class which has some permissions defined in it's Meta class, such as this:-
class MyModel(models.Model):
name = models.CharField(max_length=100)
class Meta:
permissions = (
('view_mymodel', 'Can view mymodel'),
)
but when I create a model that inherits from it like this:-
class MySubModel(MyModel):
pass
it doesn't inherit the permissions of the superclass.
Why is this and what is the best way to solve this problem without changing the MyModel class?