Is it possible to extend the model in another application without explicitly changing firs-one?

216 views
Skip to first unread message

Ol P

unread,
Feb 27, 2020, 11:34:12 AM2/27/20
to Django users
Imagen we have app-A and app-B with model-A and model-B accordingly.
And we want to extend model-A in app-B.
What should be written in model-B to implement it?

In other words, is it possible to implement the same-table extension?

wanbao jin

unread,
Feb 27, 2020, 11:54:37 AM2/27/20
to django...@googlegroups.com
Not sure if I understand the question correctly, but are you trying to use model-A in app-B?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/035f39c8-cc71-4ae5-81ed-53f2c0d4aee3%40googlegroups.com.

Ol P

unread,
Feb 27, 2020, 12:03:02 PM2/27/20
to Django users
I want to extend model-A from app-B without modification of app-A
For example, add a field or new methods or override existing.

On Thursday, February 27, 2020 at 6:54:37 PM UTC+2, Jin wrote:
Not sure if I understand the question correctly, but are you trying to use model-A in app-B?

On Fri, Feb 28, 2020 at 12:33 AM Ol P <o...@global-mediator.com> wrote:
Imagen we have app-A and app-B with model-A and model-B accordingly.
And we want to extend model-A in app-B.
What should be written in model-B to implement it?

In other words, is it possible to implement the same-table extension?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

wanbao jin

unread,
Feb 27, 2020, 12:11:20 PM2/27/20
to django...@googlegroups.com
One-time class definition is always good choice, I won't do it like that.
But can you share the detailed scenario you are gonna implement?

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9e98c0dc-547c-4b99-8bc3-f31187444f98%40googlegroups.com.

Ol P

unread,
Feb 27, 2020, 12:33:40 PM2/27/20
to Django users
I try to figure out if it possible to implement the next scenario:

garage (app-A)
 models
   
class Vehicle(models.Model)
     vin
=models.CharField(max_length=50)
     
...

     
def import_data(self):
       
...
       
# import vehicals


external integration
(app-B)
 models
   
class ExtendedVehicle(Vehicle)
     external_field
=models.CharField(max_length=50)

     
def import_data(self):
        super(ExtendedVehicle, self).import_data()
        self.import_external_field()

     def import_external_field(self):
        ...
        # import external_field


Something like this.


On Thursday, February 27, 2020 at 7:11:20 PM UTC+2, Jin wrote:
One-time class definition is always good choice, I won't do it like that.
But can you share the detailed scenario you are gonna implement?

On Fri, Feb 28, 2020 at 1:03 AM Ol P <o...@global-mediator.com> wrote:
I want to extend model-A from app-B without modification of app-A
For example, add a field or new methods or override existing.

On Thursday, February 27, 2020 at 6:54:37 PM UTC+2, Jin wrote:
Not sure if I understand the question correctly, but are you trying to use model-A in app-B?

On Fri, Feb 28, 2020 at 12:33 AM Ol P <o...@global-mediator.com> wrote:
Imagen we have app-A and app-B with model-A and model-B accordingly.
And we want to extend model-A in app-B.
What should be written in model-B to implement it?

In other words, is it possible to implement the same-table extension?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/035f39c8-cc71-4ae5-81ed-53f2c0d4aee3%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

One Above All

unread,
Feb 27, 2020, 12:55:51 PM2/27/20
to django...@googlegroups.com
It is indeed possible, but I am not sure whether it should be done this way. 

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d3dce9e3-bd95-495e-85b3-a359c21dc95e%40googlegroups.com.

One Above All

unread,
Feb 27, 2020, 12:56:03 PM2/27/20
to django...@googlegroups.com
I am not sure if this is the right way. 

Ol P

unread,
Feb 27, 2020, 12:58:42 PM2/27/20
to Django users
This is definitely not correct implementation. Only for illustration purposes.

Naveen Arora

unread,
Feb 28, 2020, 7:23:17 AM2/28/20
to Django users
Clearly Possible, 
Simply import the model first using appname.models. Hope it helps:)
You can use this as
from appA import model1
in app2

Ol P

unread,
Feb 28, 2020, 7:50:12 AM2/28/20
to Django users
But how to add fields to it?

Where to put what:
from appA import model1


class ExtendedModel1(?):
   
?

    new_field
= models.CharFiled()

   
class Meta:
       
?

Naveen Arora

unread,
Feb 28, 2020, 7:53:45 AM2/28/20
to Django users
What are you trying to achieve with this implementation?
and What do you call a extended model ?
Instance of model1 is accesible in app2, this already means you got your model1 in app2.

Curious.

Ol P

unread,
Feb 28, 2020, 8:03:20 AM2/28/20
to Django users
What are you trying to achieve with this implementation?
I want to achieve modularly and loose coupling. Separate responsibility in other words.

and What do you call a extended model ?
By extended model, I mean pat of an existing model, that implemented in one place (in out example app-A) but extended, altered or modified in another please (in out example app-B)

Instance of model1 is accesible in app2, this already means you got your model1 in app2.
Yes, but not the model itself.

Motaz Hejaze

unread,
Feb 28, 2020, 1:27:26 PM2/28/20
to django...@googlegroups.com
You should understand that extending a model will create a new table with columns existed in parent in addition to new columns you define , so yes you can extend .. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/851d7bd0-4821-4b78-bfbf-881f0db090f5%40googlegroups.com.

osasere igbinoba

unread,
Feb 29, 2020, 8:15:48 AM2/29/20
to Django users
good morning am also interested in joining the class

Naveen Arora

unread,
Feb 29, 2020, 11:55:22 AM2/29/20
to Django users
Hi again,

According to what i have understood the problem till now. You are trying to implement it the wrong way. This is not python, Everything is already built using classes, you are already overriding a class, then extending an overriden class seems complicated. Modularity and loose coupling can be achieved through a completely different way here. Be it clear, a model creates a table in the database. Why are you trying to create the same table again and again with more or less attributes ? 
Try implementing manytomany fields, foreign =key etc. This may solve your problem .

Regards
Naveen Arora

Ol P

unread,
Feb 29, 2020, 12:22:45 PM2/29/20
to Django users
This is not extending but inheritance. It will create a new table. I need to alter the existing table.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Ol P

unread,
Feb 29, 2020, 12:36:30 PM2/29/20
to Django users
Why are you trying to create the same table again and again with more or less attributes ?
I do not try to RE-create the table but to alter the existing one.

Sometimes following the pure pythonic way may limits what framework can do. But it does not mean that somewhere in the universe of the solutions there is a better way to organize things.
What I trying is to check if it is possible to use the same concepts implemented in Odoo ORM (https://github.com/odoo/odoo). I want to migrate from Odoo to Django due to the first has some limitations.
Reply all
Reply to author
Forward
0 new messages