I am trying to create an app for tracking team rosters that is based in the django admin (ie localhost/admin).
I would like to set it up so that I can assign certain teams to certain users, such that when they log in to the admin site, they can only see and edit the teams they have been assigned to.
I would like a user with superadmin status to be able to add and remove user access to various teams through the admin site, without having to modify the underlying code each time a new user or team is added. Is there a way to do this?
class Team(models.Model):
team_name = ...
# Whatever other attributes.
class Player(models.Model):
first_name = ...
last_name = ...
team = models.ForeignKey(Team)
# Whatever stats you want to keep on the players
You can dynamically change the query set used by the admin; you could do this to only show teams that are associated with the logged-in user.
class TeamModelAdmin(admin.ModelAdmin):
def get_queryset(request):
return Team.objects.filter(...)
Related
What I'm trying to implement is an invite system while I develop my website, but I'm new to how Django works.
For now, I want to be able to create a random string in the admin panel, have those added to the database, and then be required for a user to register. Eventually I want to create a user group system on the front end website where I can generate the strings there versus the admin panel, and then be able to send them out that way, but I'll get to that later.
I have the Model showing successfully in the admin panel, but I don't know how to make a text field that's automatically filled out anytime I create a new one and have the string be random each time.
class randomString(models.Model):
name = models.CharField(max_length=200)
random = models.ManyToManyField(get_random_string(length=6))
This is my current code which is throwing out an error, but I assumed that it would, I did it this way just to check and see if it was this simple. I have found out that it's not.
You can simply use an UUID:
from uuid import uuid4
class randomString(models.Model):
name = models.CharField(max_length=200)
random = models.UUIDField(unique=True, default=uuid4)
If the UUID is too long, you can generate a shorter string:
def generate_random():
from django.utils.crypto import get_random_string
return get_random_string(length=11)
class randomString(models.Model):
name = models.CharField(max_length=200)
random = models.CharField(default=generate_random, max_length=11, unique=True)
I have member table for registration.I want to display that table values(like firstname,lastname, email etc...) in another page (only registered person values )
I tried with id. if I give id means only that particular value is displaying but I want to registered person values
This is my member table
class Member(models.Model):
firstname=models.CharField(max_length=30)
lastname=models.CharField(max_length=30)
Email=models.CharField(max_length=50)
password=models.CharField(max_length=12)
Thanks for your time.
you are talking about a registered person in your app but there is no field in your models to note which one is registered! so First, you should make a decision about your members' login method and keep some data about them. one simple way is to make another table to save registered members, like this, may be useful.
class RegisteredMember(models.Model):
Member = model.ForeignKey(Member,on_delete=models.CASCADE)
#and other data you need to save
So now you can manage your registered members and find them and get data about them all things you need to do.
You need to add a user field so that you can get the data of a logged in user
from django.contrib.auth.models import User
class Member(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
firstname=models.CharField(max_length=30)
lastname=models.CharField(max_length=30)
Email=models.CharField(max_length=50)
password=models.CharField(max_length=12)
In your view to get the logged-in user data, you can use request.user
member = Member.objects.get(user=request.user)
print(member.firstname)
Consider these two models Keyword and Statement (model1 and model2 respectively):
#python_2_unicode_compatible
class Keyword(models.Model):
word = models.CharField(max_length=200)
statement = models.ManyToManyField(Statement)
def __str__(self):
return self.word
#python_2_unicode_compatible
class Statement(models.Model):
statement_id = models.CharField(max_length=200)
title = models.CharField(max_length=200)
issue_date = models.DateField("Issue-Date")
author = models.ForeignKey(Person)
released_by = models.ForeignKey(Organization)
kicpairs = models.ManyToManyField('KeywordInContext')
So on the admin site right now, the only way one would be able to determine what keywords are associated with each statement is that they have to go check the Keyword model in admin, and check each Keyword's display page and scroll through the menu.
At least with regards to the admin site, it's important for someone to be able to see a Statement model's display with all of its associated Keywords visible, and for users to be able to choose additional Keywords within the database (or make new ones). I also hope to be able to have a Statement's keywords modifiable on the admin page via the filter_horizontal widget, since that seems to be the most user friendly.
But I'm having trouble just starting with that. I'm not sure what I need to use or how.
from django.contrib.auth.models import User
from django.db import models
class Team(models.Model):
team_name = models.CharField(max_length=255, unique=True) ## they can create a new team here
description = models.TextField()
slug = models.SlugField(unique=True)
user = models.OneToOneField(User, blank=True, null=True) ## I connect my User here, so they can "own" this info...
Users have the option to create a new team (via the 'team_name' field), but I also want to give users the option to join another team (created by a different user), can I just add a new field to my Team model? And preferably, on the form, they could see a list of created teams, check the one they want to join and save the form.
Could I add something like this to my Team class:
team = forms.ModelMultipleChoiceField(queryset=Team.objects.all()).filter('team_name')
or maybe something like:
TEAM_CHOICES = Team.objects.all()).filter('team_name')
team = forms.CharField(widget=forms.Select(choices=TEAM_CHOICES))
And, how would that data be saved correctly?
If you wanted to allow users to join a team, I'd have a team attribute on the user model that was a FK to the Team.
Then in a form, you could define the list of available teams as you've suggested;
team = forms.ModelMultipleChoiceField(queryset=Team.objects.all())
And I'd also suggest looking at Select2 which creates a javascript 'search' type field that will filter the teams when someone starts to type characters.
Furthermore, the user field in your Team model would be better named as creator or owner so that you don't confuse them with a standard user associated with the team.
I'm trying to replicate Groupon using Django. I'm basically trying to achieve the below
1. A Merchant can register with the site
2. Can start creating campaigns
For the merchant registration part , I'm importing the User model and including a random MerchantID generator using random.
class Merchants(models.Model):
merchant = models.OneToOneField(User)
MerchantID = models.CharField(max_length = 15)
Storename = models.CharField(max_length = 25)
def save(self):
self.MerchantID = MerchantIDgen()
super(Merchants,self).save()
def __str__(self):
return self.merchant.username
The merchant is able to successfully register with the site. However, I'm having trouble enabling the create campaigns part.
the html link to the create campaign part is:
{% if user.is_authenticated %}
<a href = '/Merchants/{{user.username}}/Campaign'> start a campaign </a><br/>
ideally , I would want the variable portion to pull in the MerchantID value from the user. But I cant seem to pull this value from the User model( which makes sense since its sitting in the Merchants table)
In order the accomplish the above, is it better for me to just scrap the User model and just do the registration from the Merchant Model alone? (am I right in assuming that by doing so , I wouldn't have access to user authentication etc?)
Or is there any other way to pull the MerchantID using the user model?
figured a workaround which basically is to filter using username from user and using that instance to filter the Merchants table