How to add a lot to one connection with a model from an external application in django

My django project uses the django-helpdesk application. This application has a Ticketmodel.

My application received a model Clientthat should have one relation to tickets, so I could, for example, list all the tickets related to a particular client.

Normally I would add models.ForeignKey(Client)in Ticket , but it is an external application, and I do not want to change it (future problems with the update, etc.).

I have no problems with ManyToManyor OneToOne, but I don’t know how to do it with ManyToOne(many tickets from an external application to one client from my application)

+4
source share
2 answers

An even more hacky solution . You can do the following in module level code after Clientclass:

class Client(models.Model):
    ...

client = models.ForeignKey(Client, related_name='tickets')
client.contribute_to_class(Ticket, name='client')

I did not test it completely (I did not do any real database migrations), but the correct descriptors were added to the class ( ReverseSingleRelatedObjectDescriptorfor Ticketand ForeignRelatedObjectsDescriptorfor Client); South recognizes new fields. So far, it works as usual ForeignKey.

: . Django . , , . , - . , , . , , - .

+2

( ) :

. TicketProfile, " " Ticket Client.

Hacky solution. "--" ticket_id .

+1

Source: https://habr.com/ru/post/1535940/


All Articles