How to get exact values of the foreignkeys in django admin - django

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)

Related

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)
...

limit_choices_to within Django model

I have a project model. This project contains persons (those who are working on the project). I am trying to also make a model for each project person, including any notes they have on the project and % complete on project.
My issue is that I want to filter the individual_person_in_project to only the persons within the corresponding project. I am trying to use
limit_choices_to = {'person_in_project':User}
I want to limit my choices to users who are persons in my Project model.
class Project(models.Model):
project_name = models.CharField(max_length = 120,null = False,blank = False)
project_percent_complete = models.IntegerField(blank = True,null = True, default = 0)
person_in_project = models.ManyToManyField(User,related_name = 'project_person',blank = True)
project_description = models.CharField(max_length = 300,null = True,blank = True)
class Project_Person(models.Model):
corresponding_project = models.ForeignKey(Project,related_name = 'corresponding_project_this_user_is_in',null = False)
individual_person_in_project = models.ForeignKey(User, related_name = 'a_person_within_the_corresponding_project', limit_choices_to = {'person_in_project':User})
percent_complete = models.IntegerField(default = 0)
I left a comment above, but I think this is a better answer, anyhow:
You can use the through option to track extra information on the manytomanyfield, so you get:
class Project(models.Model):
...
person_in_project = models.ManyToManyField(User, related_name='project_person', blank=True, through=ProjectPerson)
The docs explain the rest of the details, but you shouldn't have to handle the limit_choices_to in that case.
Thank you for your help, it was very useful . The most helpful comment was ryanmrubin and the use of through with ManyToManyField to facilitate their relationship I ended up creating a separate class and associating that with a project.
If I need to tie more information into this new class I will certainly use through with the ManyToManyField.
class Project(models.Model):
project_name = models.CharField(max_length = 120,null = False,blank = False)
project_percent_complete = models.IntegerField(blank = True,null = True, default = 0)
project_description = models.CharField(max_length = 300,null = True,blank = True)
people_in_project = models.ManyToManyField(User,blank = True)
class Project_Tasks(models.Model):
description = models.CharField(max_length = 120,blank = True)
percent_complete = models.IntegerField(default = 0)
user = models.OneToOneField(User,unique = True,blank = True, null = True)
project = models.OneToOneField(Project,unique = True, blank = False, null = True)

Django ForeignKey OR statement

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?

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)