3

So first off, I want to clarify that I am trying to make One-To-Many relationships, not Many-to-One. I already understand how ForeignKeys work.

For the sake of the discussion, I've simplified my models; they're much more field-rich than this in the real implementation.

I have a model, called a ColumnDefinition:

class ColumnDefinition(Model):
    column_name = CharField(max_length=32)
    column_type = PositiveSmallIntegerField()
    column_size = PositiveSmallIntegerField(null=True, blank=True)

I think have a registry. Each registry has a separate set of columns for it's input and output definition. I've put the theoretical "OneToManyField" in there to demonstrate what I'm trying to do.

class Registry(Model):
    input_dictionary = OneToManyField(ColumnDefinition)
    output_dictionary = OneToManyField(ColumnDefinition)
    created_date = DateTimeField(auto_now_add=True, editable=False)

A ColumnDefinition is only ever related to one Registry ever. So it's not a Many to Many relationship. If I put a ForeignKey on the ColumnDefinition instead to create a reverse relationship, it can only create a single reverse, whereas I need both an input and output reverse.

I don't want to have to do anything kludgey like adding a "column_registry_type" field onto ColumnDefinition if I can get around it.

Does anyone have any good ideas on how to solve this problem?

Thanks!

4
  • What exactly is the problem? What do you mean by "reverse"? Django automatically creates reverse relationships in its Model class (ORM) methods. Sep 4, 2013 at 21:16
  • The problem is that I need two "reverse relationships" that are distinct. If I put a ForeignKey on ColumnDefiniton to Registry, it only creates one. Sep 4, 2013 at 21:19
  • The only thing I can think of is to have two separate ColumnDefinition models, perhaps? Sep 4, 2013 at 21:21
  • OK good clarification, thanks. Me thinks that with the two answers below you should arrive to a solution. Sep 4, 2013 at 21:45

2 Answers 2

1

You can add two ForeignKeys on ColumnDefinition, one for input and one for output, and give them separate related_names:

class ColumnDefinition(Model):
    ...
    input_registry = models.ForeignKey(Registry, related_name='input_columns')
    output_registry = models.ForeignKey(Registry, related_name='output_columns')

You can then access the set of columns like registry.input_columns.

3
  • I guess I could do that, but I would need to make sure that they were always mutually exclsuive. A ColumnDefinition cannot ever be associated with two registries. Sep 4, 2013 at 21:37
  • Well, the other way to do this would be to use a single ForeignKey and then have a separate column indicating its type (input or output). But you said you wanted to avoid that... Sep 4, 2013 at 22:00
  • Hmm, looks like @Peter and I are giving you the same answers at the same time. :-) Sep 4, 2013 at 22:01
0

You can and should define two different ForeignKey fields on ColumnDefinition. Just make sure to specify a related_name value for at least one of them. https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.related_name

2
  • This seems to add two kludgey fields where one will always be empty, or worse, if both of them are defined somehow it would cause a meltdown since they're not supposed to be "TwoToOne" related. Sep 4, 2013 at 21:34
  • It's either that or define a single foreign key and use a different field to indicate the relationship type, maybe also adding some methods on Registry to help support your reverse lookups. Did you have a database representation in mind? That can help guide your model design in fringe cases like this. Sep 4, 2013 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.