0

I checked to see if this is possible, but the documentation is not forthcoming. I also have not seen any questions which seem to address the issue.

I have a model:

class CreditCard(models.Model):

    NAME = models.CharField(max_length = 55)
    TYPE = models.CharField(max_length = 12)
    NUMBER = models.CharField(max_length = 16, primary_key=True)
    CCV2 = models.CharField(max_length = 4)
    EXPMONTH = models.CharField(max_length = 2)
    EXPYEAR = models.CharField(max_length=4)
    **USER_ID = models.ForeignKey(User, to_field="USER_ID")**
    ADDRESS = models.ForeignKey(Address, to_field="ADDRESS_ID")

The field in question is surrounded in asterisks.

I want this field to be able to reference one of two tables resembling a ForeignKey OR statement:

USER_ID = models.ForeignKey(User OR Merchant, to_field="USER_ID" OR "MERCHANT_ID")

Is this possible? If so, how is it done?

Machavity
  • 30,841
  • 27
  • 92
  • 100
karnesJ.R
  • 326
  • 1
  • 11
  • 3
    See the documentation on [generic relations](https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#generic-relations). – Kevin Christopher Henry Jul 20 '14 at 07:44
  • Yeah.. and don't use UPPERCASES in model variables. – Sławek Kabik Jul 20 '14 at 08:20
  • Just wondering, why not? I'm accustomed to using UPPERCASE as table columns in SQL. – karnesJ.R Jul 20 '14 at 08:46
  • You can do what you like, but since you're writing in Python it's probably better to adopt its naming conventions. More importantly, there's the potential for confusion since model *attributes* are different from SQL *fields*. For example, the attribute you've named `ADDRESS` corresponds to a field named `ADDRESS_ID`. And in the case of a `ManyToManyField` there is no corresponding SQL field at all. – Kevin Christopher Henry Jul 20 '14 at 17:14

0 Answers0