I have been working with Django for a while now and I think I'm getting the hang of it. Working with database data is pretty easy with models. However, I am really struggling to find a way to handle data for which making a model isn't really an option. For example, I want to have a "welcoming" text on my homepage that should be editable from the admin interface. Other examples are an email address for contact information, a path to a file (eg resume) stored on the server, links to social media, ...
Making a model for this kind of data seems unnecessary to me, as there will only be one entry in the table for that model. Up until now I have been using Constance for a while, which is pretty nice. However, I'm interested to find out how I should handle this kind of data with "vanilla" Django
Why not have a model for bits of text to be displayed on your site. It could simply have a unique name, and a text field.
from django.db import models:
class SiteText(models.Model):
name = models.CharField(max_length=64, unique=True)
text = models.TextField
Then it's easy to create a simple bit of text in the admin panel, or in code:
SiteText.objects.create(name="greeting", text="Welcome to my site!")
And accessing that from within your site is as simple as:
SiteText.objects.get(name="greeting")
It's fine if this model has only one instance, but it also allows you to store other snippets of text in the database and easily access them from other parts of your codebase.
For your other examples, paths to resumes, email addresses, links, these all sound like they could be fields on a user model, or separate tables with foreign keys to a user. Storing things like this in the database is perfectly reasonable.
Related
I have created a simple app in Django using some tutorials, but it became very usefull and also scaled a lot.
What i have is a basic HTML table - prefilled with data from Model A.
At the end of the table there is Submit button, which just use some javascript to prompt a print window (to save the table[page] as PDF basically)
What i would like to do, is that when i press the Button to print, i would also pass some data from the table for example ModelA name and adress into a ModelB - which would serve as an statistic.
However i have used for this a simple tutorial, and therefore to display the table i used "DetailView" in views.py. This is my views.py file
class PrinterDetailView(DetailView):
model = Printer
template_name = 'printer_detail.html'
Is it possible to achieve this without redoing the whole site? i Found some people searching for simillar answers, but from that it seemed like i would have to redone whole app..
Thanks for your input!
Let's say I have a django project of a social network and this site has many users. All the users come from django.contrib.auth.models.User class. Now one user let's say writes a blog and in it, mentions another user with '#' similar to of twitter. So exactly how to do this? What kind of approach should be taken?
And coming away from just user model, rather how to do this with any custom model also? Like if I have a Blog model and each blog has a title, how to mention with that title? And most importantly this mentioning should be an automatic thing. Like If one user's name is "PhantomWarrior", then if one writes "#Phant" and he is still writing, it should automatically predict the username "PhantomWarrior" and give the user option to select that for mentioning.
Similarly how to do this with the title of a blog?
I found this post: how to mention/tag users with '#' on a django developed project in stackoverflow talking about this thing but the answer kind of did not satisfy what I am wanting.
So how to do this? Any help will be much appreciated.
You can do it in two ways.
In any cases you need add m2m field from blog post to user model.
Simplest solution. After any creation or changes in model you need to check content and parse text for all #NAME patterns, for example using regexp. After this you need search all founded patterns in user model and add it to m2m field in blog post.
This solution used in most cases like you wrote. You need add frontend component, that open autocomplete every time, when you write # in text boxes. This autocomplete component should be connected to api method, that returns list of users names and IDs, by search pattern. When you select user in autocomplete you need store his ID in some storage on frontend side. When form will be send, you send user IDs with all other form data, and save it in m2m field in blog post.
I created website-app for myself and it become really useful and many peoples wont also use it. But user management was not scheduled in application architecture. Is there any way to easy add user registration so each user would have his own google database tables but with same name?
example of one "table":
class Settings(db.Model):
email = db.StringProperty()
link = db.LinkProperty()
rating = db.StringProperty()
How can I separate data from this "table" between different users? I search for some kind of wrapper so I don't need to change current architecture.
You have to remember there is no concept of tables with the datastore so you can't have a separate set of tables for each user as such.
You have a few choices, the two I would investigate are
create a separate app with the existing code base and each user runs their own site. You may not need to do any code changes at all
If you want complete separation of data for each user in a single app then look at namespaces (thats how multi-tenancy is normally implemented.)
However you haven't really provided a clear definition of how you want to separate the users etc.. so there a probably other approaches you can take.
Short of having a copy of the database for each user; you'll have to implement some code changes.
The easiest one you can do is add a foreign key to your data tables that points to your user table; then you filter all records based on this foreign key.
Once you have that change, you can write a view decorator that will automatically filter the records for you.
Well, I do my first steps with Django and Django REST framework. The problem I face is that all examples throughout the whole Internet are based on hard-coded models. But the whole concept of models frustrates me a little bit, because I'm used to deal with different data which comes from numerous sources (various relational databases and nosql - all that stuff). So, I do not want to stick to a particular model with a fixed number of predefined fields, but I want to specify them just at the moment when a user goes to a particular page of my app.
Let's say I have a table or a collection in one of my databases, which stores information about users - it has any kinds of fields (not just email, name and likewise - all those fields as in all those examples throughout the web). So when a user goes to /users/ I connect to my datebase, get my table, set my cursor and populate my resultant dictionary with all rows and all fields I need. And REST API does all the rest.
So, I need a "first-step" example wich starts from data, not from a model: you have a table "items" in your favorite database, when a user goes to /items/, he or she gets all data from that table. To make such simplistic api, you should do this and this... I need this kind of example.
I think the key is to use the models differently. If you use onetomany or foreignkey references in your model construction you can more dynamically link different types of data together, then access that from the parent object.
For example, for your user, you could create a basic user model and reference that in many other models such as interests, occupation, and have those models store very dynamic data.
When you have the root user model object, you can access it's foreign key objects by either iterating through the dictionary of fields returned by the object or accessing the foreign key references directly with model.reference_set.all()
I am a bit new to GAE, so I wanted to ask, how do you create a user model for something as trivial as a blog.
So, far, this is all I got:
class User(db.Model): # Todo create a user model, that works with your login page
username = db.UserProperty()
password = db.StringProperty()
But this looks very primitive. How would one go about creating a user in google app engine, as in what is the best practice, of creating such fields where uniqueness is an important factor.
Also, how would you link a user to a blog post, since links are not allowed in google data-store.
Finally is there an app that manages all your migrations? Like south for GAE? There seems to be a way to migrate, but it involved a great deal of boiler-plate code.
Also, I come from a django background, so I am finding all of this a little counter-intuitive, since this is not a relational database.
Your User class is fine for now, you can add fields to your model as you need them during development. But you should use ndb rather than db. ndb handles much more features (like automatic caching). You can find more details in the documentation.
If you want to learn more advanced user or datastore models, you can look at gae-boilerplate, which already handles user login and signup (even from facebook/twitter/...). The README is very well documented :
https://github.com/coto/gae-boilerplate
You can implement some kind of relations in your models, either by using a KeyProperty() or by setting a parent entity when you create one. For example :
class BlogPost(ndb.Model):
title = ndb.StringProperty()
content = ndb.TextProperty()
timestamp = ndb.DateTimeProperty(auto_add_now=True)
# Then you can specify a parent in the constructor (where user is a User entity)
blog_post = BlogPost(parent=user)
blog_post.title = 'Post title'
blog_post.content = 'Post content'
blog_post.put()
Then use Ancestor queries to retrieve all blog posts published by a user.