Django ForeignKey OR statement - django

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?

Related

How to get exact values of the foreignkeys in django admin

class Mio_terminal(models.Model):
terminal = models.CharField(max_length = 50)
gate = models.CharField(max_length = 50)
gate_status = models.CharField(max_length = 50, default = 'open') #open, occupied, under_maintenance
class Meta:
unique_together = [['terminal', 'gate']]
class Mio_flight_schedule(models.Model):
fact_guid = models.CharField(max_length=64, primary_key=True)
airline_flight_key = models.ForeignKey(Mio_airline, related_name = 'flight_key', on_delete = models.CASCADE)
source = models.CharField(max_length = 100)
destination = models.CharField(max_length = 100)
arrival_departure = models.CharField(max_length=12)
time = models.DateTimeField()
gate_code = models.ForeignKey(Mio_terminal, related_name = 'terminal_gate', null = True, on_delete = models.SET_NULL)
baggage_carousel = models.CharField(max_length = 100)
remarks = models.CharField(max_length = 100)
terminal_code = models.ForeignKey(Mio_terminal, related_name = 'airport_terminal', null = True, on_delete = models.SET_NULL)
These are models for terminal and flight schedules.
enter image description here
enter image description here
I want to have a terminal name and gate code instead of the object ...
I know we can get this by using the str method in models....but we get only a single value for this...not more than one
I want to use the terminal as a foreign_key for terminal_code in the flight_schedule model and the gate as a gate_code.I am getting terminal_code _gate code as a string ...but it's not reflecting as separate entities...I don't want the combined string.....I want when I click on the terminal ...only the terminal dropdown should display..and the same for the gate code
please let me know how should I deal with this.
You can change the display name of models in django admin interface by using def __str__(self).
So for in your code add this function after the Mio_terminal class :
class Mio_terminal(models.Model):
terminal = models.CharField(max_length = 50)
gate = models.CharField(max_length = 50)
gate_status = models.CharField(max_length = 50, default = 'open') #open, occupied, under_maintenance
class Meta:
unique_together = [['terminal', 'gate']]
# add this
def __str__(self):
return str(self.terminal+' '+self.gate)

M2M relationship in Django and intermediate table contain extra field

I have to models with M2M relation which are clinic and Doctor but the third custom intermediate table with additional field shift.
class ClinicHospital(models.Model):
name = models.CharField(max_length = 256)
address = models.TextField()
contact = models.CharField(max_length = 15)
lat = models.FloatField()
lon = models.FloatField()
class Doctor(models.Model):
name = models.CharField(max_length = 256)
speciality = models.CharField(max_length = 256)
contact = models.CharField(max_length = 12)
speciality = models.ForeignKey(Speciality, on_delete=models.CASCADE)
clinic_hospital = models.ManyToManyField(ClinicHospital, through='DoctorHospital')
intermediate table is
class DoctorHospital(models.Model):
clinic = models.ForeignKey(ClinicHospital, on_delete=models.CASCADE)
doctor = models.ForeignKey(Doctor, on_delete=models.CASCADE)
shift = models.CharField(max_length = 10)
Problem
First, DoctorHospital table is not shown/created in DB.
Second, when I save Clinic it does but Doctor does not save, it returns error doctorhospital is not exist.
this error view show when save Doctor

How to return return object(s) that gives me document as well as reports details

I have multiple documents and each document has multiple supporting reports. How do i return object(s) that provides me information of both document and the report
One solution is to return all the document objects and iterate over reports for each object (slow). What I am currently doing is returning document object and I have a separate page which loads onclick and returns the corresponding reports
models.py looks like this:
class Document(models.Model):
code = models.CharField(max_length = 50)
path = models.CharField(max_length = 500)
date_of_submission = models.CharField(max_length = 50)
type = models.CharField(max_length = 50)
title = models.CharField(max_length = 200)
department = models.CharField(max_length = 50)
subject = models.CharField(max_length = 100)
class Report(models.Model):
document_code = models.ForeignKey(Document, on_delete = models.CASCADE)
title = models.CharField(max_length = 200)
path = models.CharField(max_length = 500)
type = models.CharField(max_length = 50)
Expected:
object(s) that has information for both report objects and document object.
You can use ManyToManyField in your models.
Check this Link please.
class Report(models.Model):
document_code = models.ManyToManyField(Document)
...

How would I properly write this query in django?

both team_signup and signedup have foreign keys from comp_name. I'm trying to list them all in one list so that I have the list of individuals that signed up as well as the teams. I'm thinking maybe some kind of join?
View:
session = request.session._session_key
cart = comp_name.objects.filter(Q team_signup__sessionid = session | Q signedup__sessionid = session)
template:
{{cart}}
models:
#for individual sign ups
class signedup(models.Model):
comp_name = models.ForeignKey(comp_name)
sessionid = models.CharField(max_length = 60, blank=True)
price = models.FloatField(max_length = 7, blank=True)
#dancer information
dancer_1_fname = models.CharField(max_length = 30)
dancer_1_lname = models.CharField(max_length = 30)
dancer_1_email = models.CharField(max_length = 30)
#for team sign ups
class team_signup(models.Model):
comp_name = models.ForeignKey(comp_name)
sessionid = models.CharField(max_length = 60, blank=True)
price = models.FloatField(max_length = 7, blank=True)
#dancer information
team_name = models.CharField(max_length = 30)
team_count = models.CharField(max_length = 30)
team_email = models.CharField(max_length = 30)
I'm probably not using the Q right but I don't quite get it, thanks
Use chain from itertools,
so first
from itertools import chain in the top of your views
Then create the queries that you want to combine
finally, call
somename = list(chain(query1, query2))
Then in you template, just treat it like you would any other queryset
Use backward relationship:
https://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward
Then You can call Your related fields like
comp = comp_name.objects.get(id=1)
comp.signedup_set.all()
comp.team_signup_set.all()

Django Model Won't Validate or Not Installed

Can you see why I am getting this model validation error?
I have the table in MySQL called 'paypal_transactions' with records.
I'm trying to copy this project over to another computer with the existing database.
Error Message:
One or more models did not validate: store.purchase: 'paypal_transaction' has a
relation with model <class 'f3.paypal.models.PaypalPaymentTransactions'>, which
has either not been installed or is abstract.
paypal/models.py
class PaypalPaymentTransactions(models.Model):
class Meta:
db_table = 'paypal_transactions'
payment_id = models.CharField(max_length = 50)
payer = models.CharField(max_length = 25)
amount = models.DecimalField(decimal_places = 2, max_digits = 8,
blank = True, default = "0.00")
currency = models.CharField(max_length = 10)
store/models.py
from f3.paypal.models import PaypalPaymentTransactions
class Purchase(models.Model):
user = models.ForeignKey(User, related_name = 'purchase_user')
product = models.ForeignKey(Design)
quantity = models.IntegerField()
paypal_transaction = models.ForeignKey(
PaypalPaymentTransactions,
default = None,
null = True,
blank = True)
This error occurs probably because of a dependency issue:
Try use the ForeignKey like this:
class Purchase(models.Model):
user = models.ForeignKey(User, related_name = 'purchase_user')
product = models.ForeignKey(Design)
quantity = models.IntegerField()
paypal_transaction = models.ForeignKey(
'f3.paypal.PaypalPaymentTransactions',
default = None,
null = True,
blank = True)