0

I am trying to figure out how to define a one-to-many relationship between two models. The difficulty here is that I do not want to touch one of the models and therefore can not use the usual ForeignKey (Many-to-one).

There are many question on this topic but they all either talk about how to do the lookup (reverse ForeignKey lookup) or they suggest to just add a Foreign Key to one side. Here is a similar question. I will use the same code example. This is what I am looking for:

class Dude(models.Model):
    numbers = models.OneToManyField('PhoneNumber')

class PhoneNumber(models.Model):
    number = models.CharField()

Usually I would go ahead and just add a Foreign Key to PhoneNumber as suggested in many answers:

class PhoneNumber(models.Model):
    dude = models.ForeignKey(Dude, related_name='numbers')

But in my situation I would like to not touch the PhoneNumber model and therefore define something on the Dude model.

The reason for this is that I am defining a model describing a special circumstance which is rarely used. If I used a ForeignKey on the (PhoneNumber) model I would have 99.9% of all instances leave this field blank. I do not really like the idea to have a field which is always blank - maybe this is my problem.

A possible work-around is to define a many-to-many field and then add some logic actually preventing 'many' on one side and enforcing that the other side is not empty.

I hope I could describe my problem clearly.

Question: Is there some way to define a one-to-many relationship? Is there a better solution with a different approach for my problem?

P.S. The only hit on this I get from the django docs is a field attribute one_to_many which, I am guessing, is used for the lookup.

jimfawkes
  • 357
  • 2
  • 12
  • Possible duplicate of [How to express a One-To-Many relationship in Django](https://stackoverflow.com/questions/6928692/how-to-express-a-one-to-many-relationship-in-django) – ptr Dec 07 '17 at 09:18
  • There is absolutely nothing wrong with having a field which is usually blank. It won't take up any significant space in your database, if that's what you're worried about. – Daniel Roseman Dec 07 '17 at 09:24
  • I actually link to that question as "similar question" in my question. The reason why the answer to that question is not satisfactory is that I do not want to touch the phoneNumber model. – jimfawkes Dec 07 '17 at 09:25
  • I am not too worried about the DB. I just do not like the idea to define a relationship for all instances when only very few actually need or even should have that relationship. But maybe this is the way to go and I am over-engineering the problem. – jimfawkes Dec 07 '17 at 09:32
  • @jimfawkes You can achieve it by ManyToMany field or creating seperate models but all those create bigger overhead than adding a Foreign Key in PhoneNumber model. – Saji Xavier Dec 07 '17 at 09:33

1 Answers1

0

If I used a ForeignKey on the (PhoneNumber) model I would have 99.9% of all models leave this field blank. I do not really like the idea to have a field which is always blank - maybe this is my problem.

It kind of is. To define a one-to-many you use ForeignKey, and this will need to be on the PhoneNumber model. This doesn't provide any drawbacks to your approach, as far as I can see. It sounds like the main issue is that you're used to using a library that does this the other way round, and so doing it this way round seems strange to you.

I'm afraid I don't understand what you mean by

99.9% of all models leave this field blank

They can't leave it blank unless you allow them to by setting null=True, blank=True etc.

ptr
  • 3,292
  • 2
  • 24
  • 48
  • I meant to say 99.9% of all instances of that model will leave it blank. I would have to define null and blank as True, yes. – jimfawkes Dec 07 '17 at 09:34
  • I am used to define the ForeignKey on the PhoneNumber Model, is there another way to do this? I would like to do it the other way around but I do not know how. basically a reverse ForeignKey. – jimfawkes Dec 07 '17 at 09:36
  • There is no way that I know of to do a reverse ForeignKey, and I suspect any implementation would be a huge pain to maintain and keep working with core django and all it's thirdparty apps. Without a detailed explanation of your use-case I can't recommend any other solutions or approaches – ptr Dec 07 '17 at 09:39