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
Update on Comprehensive Python Cheatsheet

I apologize for submitting links that were already posted yesterday, but I would like to highlight a few things that were added since my last submission 5 months ago:

* [ABCs](https://gto76.github.io/python-cheatsheet/#type) explained with two tables.
* Table of different ways to format floats in [Format](https://gto76.github.io/python-cheatsheet/#format) section.
* In-depth explanation of [Datetime](https://gto76.github.io/python-cheatsheet/#datetime) module.
* Much extended [Class](https://gto76.github.io/python-cheatsheet/#class) section (property, dataclass, slots).
* Section on [Iterable Duck Types](https://gto76.github.io/python-cheatsheet/#iterableducktypes).
* [Metaprograming](https://gto76.github.io/python-cheatsheet/#metaprograming) section has two diagrams that nicely show relations among root classes 'object' and 'type'.

Also I would like to thank everyone who helped in any way. It was because of this subreddit that the project became so popular. I think I'm finally happy with it and will be moving on to other pet projects.

Ps: Only glaring hole that remains and I gave up on is a section about asyncio module. Maybe it will be added in the future.

/r/Python
https://redd.it/ccftbj
Are int(), float(), and type() functions or constructors?

Hi!

I'm studying Python with a C++ background.

I found int(), float(), str(), and type() are categorized as functions in Built-in Functions.

However, when I compared the types of int and abs, the results are different.

​

\>>> type(int)

<class 'type'>

\>>> type(type)

<class 'type'>

\>>> type(abs)

<class 'builtin_function_or_method'>

abs() obviously appears as a function. But int, float, str, type, etc. appear as types.

&#x200B;

The detailed description of int, float, str, type, etc. start with 'class'.

ex) copied from class type.

class type(object)

class type(name, bases, dict, **kwds)

With one argument, return the type of an object. The return value is a type object and generally the same object as returned by object.__class__.

&#x200B;

a = int(123) looks like an object construction rather than calling a casting function.

print(type(123)) looks like generating a type object and passing it to the print function so that it can be printed.

Can someone help me to understand why type(), int(), float(), etc. are functions in python?

Thank you.

/r/Python
https://redd.it/riu2gy