django-tagging like app for specific conditions in django - django

I am making a project where I want to build a user profile. In this user profile, I want to provide the field for employers and skills. Both of these fields have many to many relationship. One user can have multiple employers and each employer can have different users linked to it. The same is for skills.
To the user I want to provide a field, where it can enter the employers separated by space, and also provide Add another employer button. This is very similar to django-tagging application.
But I cannot put the tagfield there because my tags are very generic and span across different models. I am using an tagging-autocomplete on it. And I just want the employers to come in the autocomplete field and not just any tag.
This can be done using ManyToMany fields but I have not found any good tutorial that shows how I can do that.
Any help or direction is appreciated.

I belive, it is not similar how django-tagging works inside, because what you need is just a widget to select users. And this widget should have autocomplete.
Take a look at the answers on Facebook style JQuery autocomplete plugin question.

Related

Adding custom data to Django's admin dashboard

I'm having a bit of trouble with Django again.
I have a simple e-commerce website project that I'm working on for my graduation. It sells books. I've got basic functionalities down, such as adding categories and products, client sign-ups and logins, a session-based shopping cart, a checkout page fully connected to a payment API, and an orders model to keep track of data.
My professor has asked me now to to add relevant reports in the Admin panel, talked to me a while about what would be relevant to see and all. So, I've got in mind what I'm hoping to make.
I want to have two containers in the main dashboard page, which would display some quick analytics (like, how many books the store has sold in the past seven days, how much money from sales the site has made in the past month), as well as links in the sidebar: I want each relevant app within my project to have their own reports section in the Admin panel, maybe led to from a link underneath their models. I've separated the storefront, accounts, orders, shopping cart, and checkout, for instance, in different apps
The problem is I can't really figure out how to actually... do that...
I've fiddled with the layout and templates on the admin; I've figured out how to add custom links to the admin page, and change its design elements, for instance. But I'm not sure how to link the data I want to the dashboard. It feels like the answer is right in front of me and I can't reach it...
I guess my question is, how can I add my reports to the Django admin page per app, and how can I add these containers that I want in the dashboard?
I've guessed that I have to start out by building a view for each report. So I am currently reading the Django docs on the Admin page again, as well as looking at questions similar to mine.
But any information y'all can share that could ease up this process and save me some time would be very much appreciated. Thanks so much!
PS: If it helps, I am overriding the admin templates by having all the .html pages copied on my project's templates folder - it's how I got it to display the store's header in the admin dashboard.

Django - User Model for Dating site - Admin -Staff/Agency+ other users

I'm trying to make my first django app (a dating site) that consist of varying user models.
Users need to have fields like location,language,religion, height, preferences, family details horoscope etc.
Staff/Agency - users added by Admin from the panel - some contact details like address,phone etc would be enough. No self registration required.
I prefer to have email-id as the USERNAME field.
Can someone please guide me how do I proceed to make User models in this case? I have been struggling to follow the docs and various thread on forums to get some light.
Any help would be highly appreciated.
Thanks.
You should 'extend' the django user class by creating a one to one model, generally called a profile that contains the rest of the information you need to gather on a person.
It is considered bad practise and difficult to extend djangos user class directly.
Have a look at this youtube video, it's a bit out of date so don't copy it word for word, but it gets the general concept across.
https://www.youtube.com/watch?v=qLRxkStiaUg

Session or Model Field in Django?

I'm a beginner in Django. When developing an app, I want to fulfill the following functionality: There is some pictures in one webpage. A user can 'like' a picture by clicking a button bellow it. But one user can only like a specific picture once.
Now there seem two methods to do this.
1) Set an attribute in the session. So I when a user click a button, I can check if he has already 'liked' this picutre according to this session.
2) Add a new field in my user's model to record which pictures he has 'liked'.
Then I don't know which one to use. My questions are as follows:
For method (1), session can expire after some time (e.g. 2 weeks).
So for a user who revisits my website after 2 weeks, is it true that
I can not prevent him from re-liking the picture he's already 'liked'
before?
If I want to have access to the info about which pictures a user
has 'liked', is it true that I can only use method (2) to store this
information?
Thanks a lot!
If you want the "favorites" to persist across multiple sessions, then yes, you need to store the data somewhere that isn't volatile. A simple solution is to use a separate model, a LikedPicture for example:
class LikedPicture(models.Model):
user = models.ForeignKey(User, db_index=True)
picture = models.ForeignKey(Picture)
Session expired? No problem, just get the ones they've liked from the model. You could take it a step further and make the related model generic, so you don't have to make a separate model to hold each association, if you have several different models you're going to associate similarly.
Want to make sure the user only favorites something once? Django makes this ridiculously easy with get_or_create():
favorited_picture, created = FavoritedPicture.objects.get_or_create(user=user,
picture=picture, defaults={'user': user, 'picture': picture})
I find this method to be much more straightforward than trying to maintain a comma-separated-field on a model to store the ids of the favorited things.

Extend Django user model with custom fields, receivers and backend

I am designing a Django application (v1.6) and need to do several things with users:
Add custom fields, such as a foreign key for user department
Trigger database changes when certain fields change. For example, when the user's department changes I need to move inventory in another model out of the old department and into the new. I was planning to use a pre_save receiver to do this.
Define custom permissions, such as a user can only modify rows in a table that are associated with their department.
Eventually I want to integrate the application with our Active Directory server for authentication.
I looked at the options in the documentation and see that there are several options, from extending the user model with a one-to-one relationship to writing a custom user model.
What design should I use to meet all of the above goals?
Take a look at this blog post: it provides all the design principles to achieve your goals.
http://www.roguelynn.com/words/django-custom-user-models/
I would also take a look here for more information about Configurable User Models, if you want to have your own authentication scheme:
http://procrastinatingdev.com/django/using-configurable-user-models-in-django-1-5/
I also found the following reference helpful: http://www.sofokus.com/blogi/custom-user-model/

Does Django store information about who has edited and/or created a record, and if so, where?

Django has an authentication and authorization scheme baked in ('django.contrib.auth') as well as modelforms to generate forms for easy input of data into the database.
I'd like to be able to record who created a record, leveraging django.contrib.auth, with the explicit purpose of limiting editing of that same record to just that user and/or people with an "edit" permission. I know that I could use the #user_passes_test decorator to restrict access to editing my record in some fashion, but I don't know what I would compare the request.user.name to in order to determine if the current user originally created that record.
How much of this do I need to roll on my own? Do I need to capture the name author, save it to the model, and then read it - or is there something already in the framework that would do this for me?
And, if I was to attempt to save the author in a field, how would I go about doing that in such a way as to not let the user edit their own credentials?
There are a couple of apps to do something similar, please check https://www.djangopackages.com/grids/g/model-audit/
About the last questions, to prevent the user not to edit its own credentials, you can mark the field with editable=False so it wont appear in the admin or ModelForms.