How can I save & display the name of the user who submits a form saved in a database.
For example:
I have a form that allows users to review products.
I then have a table that lists these reviews.
On the table I want to list each users name besides each review.
You could add a reference field pointing to the db.auth_user table (or whatever table in which you store user data):
db.define_table('review',
...,
Field('reviewer', 'reference auth_user', default=auth.user_id, writable=False),
...)
With the above code, the default value for the "reviewer" field is the ID of the currently logged in user. The field is not writable, so the user has no way to change that (in the review entry form, you might also want to set its readable attribute to False, as there is no need for the author to see it).
Then to display the reviewer name along with the review, you could either do a join or use a recursive select (the former is more efficient if you are displaying many reviews at once, as the recursive select approach requires a separate database query to get each review's author).
Related
According to Django documentation if a field is trying to be reached in raw query set, it would fetch it in real time.
How can I prevent it from fetching fields not being retrieved from the database?
e.g. if I write select name from authors
and later a user will write author.gender it would return None and not attempt to retrieve it from the database?
You could try fetching null for any fields that you do not want to be retrieved, for example:
Person.objects.raw('SELECT id, name, null AS gender from authors')
So I have a tricky SQL query.
I have four tables:
User table which contain information on my users
Recipe table which contains information on a recipe such as the name, uploading user etc (not the ingredients)
Recipe-user associative table to link all my ingredients to the recipes
Ingredients table which holds all the ingredients that can be used
I want to create a function which downloads all this information in one go to display the users profile. This profile will consist of the users information - such as username and total recipes, with a tile view of the meals they have uploaded in a fashion similar to instagram.
So if you image the instagram user profile view:
you have the user information at the top and the images (in my case the recipes) below and when you tap a recipe you will see the list of ingredients. I want this however to be very responsive with no loading lag, hence the upfront loading of all the informaion.
Using joins this is of course not an overly difficult task, however what is produced is a messy list the length of the total number of ingredients.
I am using python with the pymysql module by the way.
I wonder if anyone has an ideas on the best possible way to go about this task?
I'm using Flask-Admin and I want to be able to update many fields at once from the list view. It seemed like what I'm looking for is a custom action.
I was able to make it work, but I suspect not in the best way. I'm wondering if it could be done more "Flask"-ily.
What I do now, for example if I was updating all rows in table cars to have tires = 4:
A custom action in the CarView class collects the ids of the rows to be modified, a callback url from request.referrer, and the tablename cars, and returns render_template(mass_update_info.html) with these as parameters.
mass_update_info.html is an HTML form where the user specifies 1) the field they would like to change and 2) the value to change it to. On submit, the form makes a POST to a a certain view (do_mass_update) with this data (everything else is passed as hidden fields in this form).
do_mass_update uses the data sent to it to construct a SQL query string -- in its entirety, "UPDATE {} SET {}='{}' WHERE id IN ({})".format(table, column, value, ids) -- which is run via db.engine.execute().
The user is redirected to the callback url.
It bothers me that I don't seem to be using any of SQLAlchemy, but (from a newbie's perspective) it all seems to be based on the model objects e.g. User.query(...), while I only have access to the model/table name as a string. Can I get some kind of identifier from the model, pass that through, and do a lookup to retrieve the on the other side?
Say I have an object which is composed of multiple pieces of information: rating, like, comment. Let's call this object a preference. Each preference would be associated with a user. That is, each user has many preferences, but each preference has only one user.
In what ways would it be better for my preference object to be structured into the design of the database, for example, as a table with columns rating, like, comment, and a foreign id key pointing to a user? A user's preference may or may not contain a rating, like, or comment, and if they don't, the entry for that specific column would be left blank.
And in what ways would it be better for my preference object to be instead assembled outside of the design of the database, by collecting each piece it needs from several tables, a table each for rating, like, and comment, and each table having a column pointing to a foreign id key of a user? If the user lacks a rating, like, or comment, that table would simply not have an entry for that user.
Specifically I will be using python and sqlalchemy to accomplish this.
You might want to look into the Entity-Attribute-Value model:
http://weblogs.sqlteam.com/davidm/articles/12117.aspx
I am setting up a simple billing system, where I have a table that lists users and the day they are supposed to be billed. Each user only has one row of this table associated with them.
I need to query the database on a daily basis to get a list of users to be billed on that day.
The model is:
class BillingDay(models.Model):
user = models.ForeignKey(User)
day = models.IntegerField(max_length=2)
How would I query against the day field? User.objects.filter(billingday=1) looks at the ID, but I'm looking i need to get a list of users with 1 as the value for day in billingday
User.objects.filter(billingday__day=1)
Just as a note, though, you might want to rethink how you're setting this up before you get too far down the rabbit hole. Will users have multiple billing days? My guess would be no. If that's the case, there's no reason for a BillingDay model. It only adds complexity and fragments data. The billing day could just be a field on your user profile.
Now, creating a user profile for a User is in principle no different that having a BillingDay model as a way to add extra data to User, but it's far more extensible. Django has builtin methods for having a user profile associate with every User, and you can more data to the same user profile object over time. Whereas, BillingDay would be relegated to just one data point and you'd later have to add additional models (more complexity and fragmentation of data) for other data points down the line.
See Django's documentation on user profiles.