Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Let's say I have a database called "A", this database has only two records "name: Jhon" and "name: Sara", and I also have the database "B" that has the names "Jhon" and "Sara" among other names, how can I search the database B only for the names Jhon and Sara?
Context: The database A has specific values, the database B is user generated, what I'm trying to accomplish is when the user-generated record to B database matches the A's record add some points to the user, it's like a game or something.
How can I accomplish this?
Generally, a django model maps to a single database table.
Each attribute of the model represents a database field.
the following example retrieve a list of names from the first Model (named "A"), then searches into "B" for any record having the value of name attribute in that list.
names_to_search = A.objects.all().values_list('name', flat=True)
people = B.objects.filter(name__in=names_to_search)
I assume that both A and B have a field called name.
A database has tables, which have attributes.
In this case, you have 2 tables A and B.
You have "name" attribute for each table.
I do not understand what you are trying to do, but if you want to query a table with a name in Django,
person = A.objects.get(name=myName)
So if you want to know if Jhon is in table A,
try:
person = A.objects.get(name="Jhon")
#Jhon exists.
except ObjectDoesNotExist:
#Jhon does not exists.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I practice coding with html and django and I have a project "Movie Reviews" and I have some question.
how to show category for user who often click that category?
e.g
You often like to chose Romantic category.Website should show romantic category to you when you come to this website again.(already login)
sorry for my English.I'm beginner for English and Coding.
Thankyou
This question is very wide, but this should help you; I guess you already have a Category and a Movie model, right?
Those two are probably related trough a one-to-many relationship. Here you should do something similar, but you'll need a many-to-many relationship.
The simplest way of doing it would be to create a new model, for example 'CategoryVisited', with 3 fields: user_id, category_id, and count.
Then, each time a user visits a category, you'll either create a new CategoryVisited or get the existing one matching the current user_id and category_id, and increment the 'count' field.
At last, to retrieve the preferred category of a user, you'll have to write a simple 'max' request to get the category_id with the max count for the current user.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
What sql query can be used to search for a variable in a table, with the aim that the variable isn't there. How do I know if a value is in my table? Because I haven't found a search query yet.
I tried making the sql query a varible and setting it to true/false but that didn't work.
I created the table in sqlite it has the following fields ('CustomerID', 'UserName', 'FirstName', 'Password', 'Email') . I'm doing validation and I want to check if a username has already been used.
You could use a SQL check something like...
SELECT 1 AS userExists
FROM yourTable
WHERE userName = 'JoeBloggs'
LIMIT 1
(Return the value 1 for each row where the username is JoeBloggs, but limit the results to just one row.)
Then, in the python, check how many rows it returns. 0 rows = it doesn't exist, 1 row = it exists at least once.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a table named 'student_info' in a Postgres database. The table has coloumns
'student_id', 'student_name', and 'student_age'.
I have written three Python functions namely : student_id(), student_name(), student_age() returning student_id, student_name, and student_age respectively.
I want to write these values into respective columns in the table from SQLAlchemy.
I am not getting any information on how can I do it.
If you are beggining with sqlalchemy you can follow the tutorial, it will guide you to every step of connecting to your database, create mapping entity, and perform basic operations on your tables with sqlalchemy.
if you already know how to connect and have a mapped entity to represent your table you can simply instantiate your models, add data into each fields then use the add method like this:
student = Student(student_id=1, student_name='foo', student_age=15)
session.add(student)
session.commit()
if you dont want to use sqlalchemy mapped entity you can perform raw sql using an sqlalchemy engine:
db_url = 'your_database_url'
engine = create_engine(db_url)
engine.execute('your_sql_query')
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm using SQLite and Python but i want to insert random string that is relevant for example if i wanted a certain song genre, i want song titles that arent random letters. I'd appreciate any help...
def genre():
genre=('''What genre do you want
1.
2.
3.
''')
choice=input(genre)
'''GENERATE RANDOM SONGS BASED ON GENRE INTO PLAYLIST DATABASE'''
def length():
length=input("How long do you want the playlist to last")
'''INSERT DATA INTO PLAYLIST WHICH ADDS TO USER LENGTH'''
Query the song titles, break the words apart on each space, then push those words into your playlist database in SQLlite, and then do another for loop to retrieve those words from your database in a random order.
pseudo code
--query the song
--break string into separate words
--push each word into a list
--for loop that iterates over your list and sends each new word to your
database
--then a function that will retrieve those songs from your database
--then present them to the user.
I would learn the fundamentals first and go through the examples.
Those are the steps you would need though, many separate functions. https://www.learnpython.org my friend
http://docs.python-requests.org/en/master/
for actually getting the information from a url.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Hi my project works on Django Python. What I want to achieve is since django tasty pie doesn't support combining two resources(no relationship b/w the tables), I need to come up with my own resource. Here I have 2 tables, Table A and B. There is no relationship b/w these 2 tables. But both the tables have a field/column named gname in common. So I want to get all the distinct gnames from both the tables and put it into one list (no duplicated values) and I need to display these gnames as a list in my template. Is there anyway to do that? Thanks in advance.
try:
gnames1 = list(A.objects.values_list('gname',flat=True).distinct())
gnames2 = list(B.objects.values_list('gname',flat=True).distinct())
gnames = list(set(gnames1+gnames2))
render(request, 'sampletemplate.html', {'gnames':gnames})
Set is a data structure that doesn't allow duplicate values.
You can add all the values from both the tables to it and I think you are good to go.
Python Sets