Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
How do I use/ access a foreign key in views in Django?

I am making a programing etymology site in Django. It will have the etymology/history of programing terms. or

For example the term LET (in ECMA6):
> Let is a mathematical statement that was adopted by early programming languages like Scheme and Basic.

I am not clear on how to write views.

For instance, I want to return the term let and the text of the let terms. Let is in the Term table and text is in the Etymology table.

**EXAMPLE**

Here is what I would like to see returned for term_name=Let.

LET:

Let is a mathematical statement that was adopted by early programming languages like Scheme and Basic. Variables are considered low level entities not suitable for higher levels of abstraction, thus the desire of many language designers to introduce similar but more powerful concepts like in Clojure, F#, Scala, where let might mean a value, or a variable that can be assigned....



**MODELS**

Here are my models.

class Term(models.Model):
term_name=models.CharField(max_length=150)
term_type=models.CharField(max_length=150) #type of command
term_pub_date=models.DateTimeField('Term Pub Date')

def __str__(self):
return self.term_name

class Etymology(models.Model):
term = models.ForeignKey(Term, on_delete=models.CASCADE)
etymology_text=models.TextField()
classification=models.CharField(max_length=150) #prog or tool reference
etymology_votes = models.IntegerField(default=0)
# rating=
# user=
etymology_pub_date=models.DateTimeField('Ety date published')

def __str__(self):
return self.etymology_text[:50] + "..."


class Comment(models.Model):
etymology = models.ForeignKey(Etymology, on_delete=models.CASCADE)
comment_text= models.TextField()
comment_votes = models.IntegerField(default=0)
#rating=
#user=
comment_pub_date=models.DateTimeField('com pub date')

def __str__(self):
return self.comment_text[:50] + "..."

How do I write a view which accesses two tables at once. How would I write the view to return what I want to see from my models?

FYI:
Using Django 2.02 and Python 3.6. Also, I not highly experienced so simple


Citation:

JavaScript?, W. (2018). Why was the name 'let' chosen for block-scoped variable declarations in JavaScript?.

/r/django
https://redd.it/8ad8pe