Doctrine2 Base entity class association mapping - doctrine-orm

I have a Base entity and it's children, for example: news, discounts, gallery, etc..
What I would like to achieve is, that the Base class has an association map to the Image entity, so all it's children are automatically joined with the Image class.
Could you please give me some advice how to achieve this? Thanks

Whenever you have an inheritance using Doctrine, you need to follow the guidelines:
http://doctrine-orm.readthedocs.org/en/2.0.x/reference/inheritance-mapping.html

Related

django multi-table inheritance, access method from child from instance of parent

I am using Multi-Table inheritance (aka Concrete Inheritance), where I have a non-abstract model + DB Table called Clients, which is concerned with common details concerning all the clients.
But a client can be an Individual, Partnership or Company, for which I have created inheriting models and tables. An Individual has first name + last name, and company has other specific particulars, etc.
I want to be able to access the names of clients (derived from the columns from the child tables) when I want a list of all clients.
After lot of searching, I found that this tutorial, works successfully.
Basically, it involves inserting a column on the Client table, which will store the name of the Child model. Then using that name, the appropriate child model is identified and appropriate child method is accessed.
But it seems to be a slightly cumbersome way to implement polymorphism in Multi-Table inheritance.
I want to know whether since 2012, Django has introduced any better way to deal with the issue, or is this still the only way?
Please let me know if my code sample is required, but the link provided has a beautiful example already.
There is django-model-utils application with Inheritance Manager. It will cast automatically your parent class to children instances. Example from docs:
from model_utils.managers import InheritanceManager
class Place(models.Model):
# ...
objects = InheritanceManager()
class Restaurant(Place):
# ...
class Bar(Place):
# ...
nearby_places = Place.objects.filter(location='here').select_subclasses()
for place in nearby_places:
# "place" will automatically be an instance of Place, Restaurant, or Bar
Also check this question for generic solution with ContentType.
And also check awesome article by Jeff Elmore about this topic. Quite old, but still great.

Refactoring Entities with common fields but with different #JoinColumn parameters

I have several entities that have same fields which are common to many tables. But those fields are not linked with the same parameter name in the relational tables(see object_1 and object_2 in the attached image).
I creacted an abstract class (IGlobalObject) and I used #MappedSuperclass to link it to subclass. Now I have to override parameters but I don't know how to do it ?
Please see the attached image for more details .
Thanks in advance

Django dynamic models

I'm pretty new to Django and Python. I'm trying to create some "category-dependent" models.
I have a Product model and i want to have category-dependent atributes. Example:
If i select "Permanent Dyes" in the category of my product i want to have specific atributes for my user to fill.
I don't want to create different models for every type of product i'm going to manage.
Is there any workaround to do this and keep using the django-admin?
Thanks in advance!
Model inheritance seems like it would help in this situation. Using it, you can have an abstract Product base class and then use Meta to add different product-dependent characteristics you need. It may require different models, but you would only need to add the attributes you needed, like below
class Product(models.Model):
....
class ProductA(Product):
class Meta:
....
This will allow you to have general properties in the Product class, such as price, etc., but use the subclass as a way to distinguish between different products.
Hope that helped in some way!

Django - how to duplicate all fields definitions from one model to another without inheritance

I need to define a extact copy of existing model with it's own table and all columns without Django inheritance mechanism. Otherwise it uses OneToOne relation and keeps all duplicated fields in parent table, that I definetly don't need. I just want to avoid repeating model fields and method definitions for my second model.
Any suggestions?
One way would be to create abstract base model with common attributes. Then create one model corresponding to parent model in current app.
Create another model for the duplicate model with same base class (and some other fields).
Not elegant though!
Did you look at Mixins?
With them you can mixin the fields of a class to your Model class and still inherit form a regular Base class. And you can mixin fileds from different classes and thus maybe make a good structure.
http://eflorenzano.com/blog/2008/05/17/exploring-mixins-django-model-inheritance/

Using instance methods with Django Model

I'm trying to implement for methods for eac Model object in my project. get_all() , get_by_id(),add(),remove() and maybe others.
I need to use them by each object . When I declare an object automatically can call one of those methods.
The problem , I don't want to duplicate the code of the four methods in each class .
Is there a way to write them once, and link them to each object.
I heared about instance methods in python.
How can use this.
Please a help
Thanks
...
You can have a base manager which has all these methods and inherit them. Django model and manager inheritance
class MyModel(models.Model):
def get_all():
return <something>
class A(MyModel):
pass
class B(MyModel):
pass
You really should seperate row-level actions (like delete(), get_some_calculated_attribute()) which go in Model classes from table level actions (like create(), filter()) which go in Manager classes.