I want to list all active users in dropdown list. But all users are listed in the dropdown
How can I do it?
template
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<input type="hidden" name="customer_id" value="{{ customer.id }}">
<button type="submit" class="btn btn-primary btn-sm">Assign</button>
</form>
forms
class AssignForm(forms.ModelForm):
class Meta:
model = Customer
fields = ('user',)
views
form = AssignForm(request.POST or None)
if request.POST:
customer_id = request.POST.get('customer_id', None)
customer = Customer.objects.get(id=customer_id)
user = UserProfile.objects.get(id=request.POST.get('user', None))
customer.user = user
customer.save()
# form.save()
return redirect('user:customer_list')
models
class UserProfile(AbstractUser, UserMixin):
company = models.ForeignKey(CompanyProfile, on_delete=models.CASCADE, null=True, unique=False)
username = models.CharField(max_length=500, unique=True)
first_name = models.CharField(max_length=200)
...
You can filter the user field in the assignForm with:
class AssignForm(forms.ModelForm):
user = forms.ModelChoiceField(
queryset=UserProfile.objects.filter(is_active=True)
)
class Meta:
model = Customer
fields = ('user',)
You can also filter this already at the model level with:
class Customer(models.Model):
user = models.OneToOneField(
'UserProfile',
limit_choices_to={'is_active': True}
)
In that case you do not need to specify this in the ModelForm(s).
I would strongly advise that you use the form in a POST request to create an instance, so:
customer_id = request.POST.get('customer_id')
if customer_id:
instance = get_object_or_404(Customer, id=customer_id)
else:
instance = None
if request.method == 'POST':
form = AssignForm(request.POST, instance=instance)
if form.is_valid():
form.save()
return redirect('user:customer_list')
# …
You can create a new model ActiveUser and impose a filter to it (see https://www.django-rest-framework.org/api-guide/filtering/), depending on what "active" means in your case.
Related
Account is my AUTH_USER_MODEL and AccountDisplayInfo consists of all the additional display info of every account. So they can input and submit, and subsequently update their information. These are my codes, but I'm unsure why it isn't working. First of all, I am receiving this error:
DoesNotExist at /account/5/displayinfo/ AccountDisplayInfo matching query does not exist.
Secondly, the "update" function isn't working.
models.py
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
username = models.CharField(max_length=30, unique=True)
class AccountDisplayInfo(models.Model):
account = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
instagram = models.CharField(max_length=50, unique=True, blank=True, null=True) #instagram
.html
<form method="POST" enctype="multipart/form-data">{% csrf_token %}
{{ form.as_p }}
<div class="d-flex justify-content-center">
<button type="submit" class="btn btn-primary btn-sm col-lg-5">Update</button>
</div>
</form>
views.py
def display_information_view(request, *args, **kwargs):
user_id = kwargs.get("user_id")
account = Account.objects.get(pk=user_id)
context = {}
displayinfo = AccountDisplayInfo.objects.get(account=account)
if request.POST:
form = DisplayInformationForm(request.POST, request.FILES, instance=request.user)
if form.is_valid():
info = form.save(commit=False)
info.account = request.user
info.save()
messages.success(request, 'Your profile display information have been updated', extra_tags='editdisplayinfo')
return redirect("account:view", user_id=account.pk)
else:
form = DisplayInformationForm(request.POST, instance=request.user,
initial={
"instagram": displayinfo.instagram,
}
)
context['form'] = form
else:
form = DisplayInformationForm(
initial={
"instagram": displayinfo.instagram,
}
)
context['form'] = form
return render(request, "account/displayinfo.html", context)
forms.py
class DisplayInformationForm(forms.ModelForm):
class Meta:
model = AccountDisplayInfo
fields = ('instagram')
Also, would be great if you can advise on this::
If I have 2 tables. Table 1 and Table 2. Table 2 has a foreign key to table 1 but table 1 dont have a foreign key to table 2. How can I query table 2's data from table 1? Thanks
By default .get() will return a DoesNotExist exception if no object matches the query you executed and stop the code from running, so if you want to input it manually on the same page use filter instead:
displayinfo = AccountDisplayInfo.objects.filter(account=account).first()
Then in your template do something like this:
{% if displayinfo %}
... show display info...
{% else %}
<p> No info yet </p> <!-- (or show some form) -->
{% endif %}
To answer your other question:
You have to use the related_name or related models attribute to access the ForeignKey data or use the model name with the _set suffix, for example:
class Post(models.Model):
title = models.CharField(max_lenght=10)
class Comment(models.Model):
body = models.CharField(max_lenght=200)
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
then you would get the Post and its comments:
post = Post.objects.get(pk=1)
comments = post.comments.all()
if you didn't have the related_name attribute in your model field you would do this instead:
comments = post.comment_set.all()
UPDATE
Maybe the issue is in your Form class, try removing the save method from it and instead do this in your view:
if request.POST:
form = DisplayInformationForm(request.POST, request.FILES, instance=request.user)
if form.is_valid():
info = form.save(commit=False)
info.account = request.user
messages.success(request, 'Your profile display information have been updated', extra_tags='editdisplayinfo')
info.save()
return redirect("account:view", user_id=account.pk)
I have a problem that I want to make a football league page that can submit the starting XI list.
But I don't know how to build the Dynamic Form.
User (which is the team owner) can Login to choose the match to upload the starting XI.
Website will show up the player list of the Team and let the User select Max of 11 and submit.
The important thing is
How can I generate the form with the Players belongs to Team.
How to let the User upload Home / Away (The team belongs to User) starting XI only.
Model.py
class Schedule(models.Model):
schedule_name = models.CharField(max_length=7, choices=LEAGUE_CHOICES, default='nil')
schedule_home = models.ForeignKey(Team, on_delete=models.CASCADE,default='',related_name='schedule_home')
schedule_away = models.ForeignKey(Team, on_delete=models.CASCADE,default='',related_name='schedule_away')
class Player(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
player_name = models.CharField('Player Name', max_length=30, unique=True)
player_team = models.ForeignKey(Team ,on_delete=models.SET_DEFAULT, default=1)
class Team(models.Model):
team_name = models.CharField('Team Name', max_length=30, unique=True)
team_owner = models.OneToOneField(User,on_delete=models.CASCADE,related_name='owner')
class Match_Starting(models.Model):
starting_schedule = models.OneToOneField(Schedule,on_delete=models.CASCADE)
home_starting = models.ManyToManyField(Player,blank=True,related_name='home_starting')
away_starting = models.ManyToManyField(Player,blank=True,related_name='away_starting')
#receiver(post_save, sender=Schedule)
def create_match_stat(sender, instance, created, **kwargs):
if created:
Match_Starting.objects.create(starting_schedule=instance)
Forms.py
class MatchStartingForm(forms.ModelForm):
class Meta:
model = Match_Starting
fields = '__all__'
Views.py
#login_required
def update_starting(request):
if request.user.is_authenticated:
try:
selected_team = Team.objects.get(team_owner=request.user)
except:
return HttpResponseRedirect('/')
if request.method == 'GET':
selected_player = Player.objects.filter(player_team=selected_team).order_by('player_name')
team_schedule_list = Schedule.objects.filter(schedule_time__lte=datetime.datetime.now() + datetime.timedelta(weeks=1),schedule_time__gte=datetime.datetime.now() + datetime.timedelta(minutes=30)).filter(Q(schedule_home__team_name=selected_team)|Q(schedule_away__team_name=selected_team))
return render(request, 'update_starting/update_starting.html',{"user":request.user,"team":selected_team,"player":selected_player,"schedule":team_schedule_list})
if request.method == 'POST':
selected_match= get_object_or_404(Match_Starting, starting_schedule__id=request.POST.get("schedule_id"))
request.POST = request.POST.copy()
if selected_team == selected_match.starting_schedule.schedule_home:
request.POST['home_starting'] = request.POST['starting']
form = MatchStartingForm(request.POST, instance=selected_match)
if form.is_valid():
selected_match = form.save(commit=False)
selected_match.save()
form.save_m2m()
elif selected_team == selected_match.starting_schedule.schedule_away:
request.POST['away_starting'] = request.POST['starting']
form = MatchStartingForm(request.POST, instance=selected_match)
if form.is_valid():
selected_match = form.save(commit=False)
selected_match.save()
form.save_m2m()
return HttpResponseRedirect('/')
update_starting.html
<div>
<form action="" method="post">
{% csrf_token %}
<select name="schedule_id">
{% for x in schedule %}
<option value="{{x.id}}">{{x.get_schedule_name_display}} {{x.schedule_home}} - {{x.schedule_away}}</option>
{% endfor %}
</select>
{% for x in player %}
<input type="checkbox" name="starting" value="{{x.id}}">{{x.player_name}}<br>
{% endfor %}
<input type="submit" class="button" value="Save">
</form>
</div>
How can I generate the form with the Players belongs to Team.
You first would need a form for players:
class PlayerForm(forms.ModelForm):
class Meta:
model = Player
fields = '__all__'
and then put this into practice: Django Formsets
How to let the User upload Home / Away (The team belongs to User) starting XI only.
You will need here a form too, this time for the Schedule model as well.
#rchurch4 said: They just want to know how to ensure that the User can only upload the roster for his team.
If that the case, you only have to be sure in your Team list view you only list teams belonging to the authenticated User.
Problem description: UserProfile form doesn't save any data.
I am creating a new User and automatically create a UserProfile object for him (so I'm extending UserProfile), so I can go to admin page and fill all the fields . But when I'm trying to do it from client side, my form just doesn't catch the data.
Also the strangest moment is that I can change username and email using UserChangeForm, so I'm trying to do the same for UserProfileObject.
models.py:
class UserProfile(models.Model):
user = models.OneToOneField(User)
image = models.ImageField(upload_to='profile_image', blank=True)
title = models.CharField(max_length=100, default = '')
first_name = models.CharField(max_length=200, default = '')
last_name = models.CharField(max_length=200, default = '')
subject = models.ManyToManyField('Subject', related_name='tutor_type', default = '', help_text="Select a subject")
AREA_STATUS = (
('Jerusalem', 'Jerusalem'),
('Tel Aviv', 'Tel Aviv'),
('Haifa', 'Haifa'),
('Eilat', 'Eilat')
)
area = models.CharField(max_length=200, choices=AREA_STATUS, blank=True, default='', help_text='Tutor area')
# Foreign Key used because tutor can only have one area, but area can have multiple tutors
# Author as a string rather than object because it hasn't been declared yet in file.
description = models.TextField(max_length=4000, help_text="Enter a brief description about yourself")
charge = models.IntegerField(default = '0')
# ManyToManyField used because Subject can contain many tutors. Tutors can cover many subjects.
# Subject declared as an object because it has already been defined.
LANGUAGE_CHOICES = (
('English','English'),
('Hebrew','Hebrew'),
('Russian','Russian'),
('French','French'),
('Arabic','Arabic'),
)
language = models.CharField('Language', choices = LANGUAGE_CHOICES, max_length=50, null=True)
def __str__(self):
return self.user.username
def display_subject(self):
"""
Creates a string for the subject. This is required to display subject in Admin.
"""
return ', '.join([ subject.name for subject in self.subject.all()[:3] ])
display_subject.short_description = 'Subject'
def create_profile(sender, **kwargs):
if kwargs['created']:
user_profile = UserProfile.objects.create(user=kwargs['instance'])
post_save.connect(create_profile, sender = User)
forms.py::
class EditProfileForm(UserChangeForm):
class Meta:
model = User
fields = (
'username',
'email',
'password'
)
class EditExtendedProfileForm(UserChangeForm):
class Meta:
model = UserProfile
fields = '__all__'
exclude = ('user',)
views.py:
def edit_profile(request):
if request.method == 'POST':
form = EditProfileForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
return redirect(reverse('accounts:view_profile'))
else:
form = EditProfileForm(instance=request.user)
args = {'form': form}
return render(request, 'accounts/edit_profile.html', args)
def edit_extended_profile(request):
if request.method == "POST":
form = EditExtendedProfileForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
return redirect(reverse('accounts:view_profile'))
else:
return redirect(reverse('accounts:edit_extended_profile'))
else:
form = EditExtendedProfileForm(instance = request.user)
args = {'form':form}
return render(request, 'accounts/edit_extended_profile.html', args)
edit_extended_profile.html:
{% extends "base.html" %}
{% block head %}
<title>Profile</title>
{% endblock %}
{% block body %}
<div class = "container">
<form method="POST">
{% csrf_token %}
{{form.as_p}}
<button type = "submit" class = "btn btn-success">Submit</button>
</form>
</div>
{% endblock %}
and it is the same template as for edit_profile view.
No traceback, no errors. Any help will be appreciated. Thanks in advance.
I have a formset like so:
class TransactionForm(ModelForm):
def __init__(self, *args, **kwargs):
try:
user = kwargs.pop("user")
except KeyError:
user = None
super(TransactionForm, self).__init__(*args, **kwargs)
self.fields["date"].widget.attrs["class"] = "datepicker"
if user is not None:
self.fields["categories"].queryset = Category.objects.get_all(user)
self.fields["account"].queryset = Account.objects.for_user(user)
class Meta:
model = Transaction
exclude = [""]
TransactionFormSet = modelformset_factory(Transaction, form=TransactionForm, exclude=("",))
View:
def transaction_create_view(request, account_id=None):
if request.method == "POST":
formset = TransactionFormSet(request.POST)
print(formset.errors)
if formset.is_valid():
for form in formset:
if form.is_valid and form.has_changed():
form.save()
if account_id is not None:
transactions = TransactionFormSet(queryset=Transaction.objects.for_account(account_id, request.user))
else:
transactions = TransactionFormSet(queryset=Transaction.objects.for_user(request.user))
transactions.form = curry(TransactionForm, user=request.user)
transactions.forms.insert(0, transactions.forms[-1])
del transactions.forms[-1]
context = {"transactions":transactions,}
return render(request, "transactions/transactions.html", context)
Model:
class Transaction(models.Model):
account = models.ForeignKey(Account)
date = models.DateField()
payee = models.CharField(max_length = 100)
categories = models.ManyToManyField(Category)
comment = models.CharField(max_length = 1000)
outflow = models.DecimalField(max_digits=10, decimal_places=3)
inflow = models.DecimalField(max_digits=10, decimal_places=3)
cleared = models.BooleanField()
class Category(models.Model):
title = models.CharField(max_length = 100)
subcategory = models.ForeignKey("self", blank=True, null=True)
user = models.ForeignKey(User)
budget = models.DecimalField(max_digits=10, decimal_places=2)
And template:
<form action="{% url 'transaction_create' %}" method="post">
{% csrf_token %}
{{ transactions.management_form }}
{% for form in transactions %}
{{ form.as_p }}
<input type="submit" value="Save transaction" />
<hr>
{% endfor %}
</form>
I input information in the empty form it produces and click on the save button, but the formset is not valid and I get this when I print the errors:
[{'categories': ['This field is required.']}, {}]
The categories in the template are represented by a <select>, where I select a category (the background is colored). Apparently it is not set though and thus I can't save, why is that? How can I fix it?
EDIT:
I have no idea why but now it works.
I reset the database re-created some data to see if that fixes things, and it did, I didn't change anything in the code.
If you override the save() in TransactionForm, you have to call save_m2m() to save the m2m objects.
https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/
I'm really stuck with this. To show my problem I created a new Django project and started from scratch, focusing only on one single form.
What I'm trying to do is to create a form with several fields of the same name. I tried using modelformset_factory to achieve this but it looks to me like it's not what I really need.
Below is my code (also on dpaste) which currently works fine with one single field called name. How can I create and process a form which would have several name fields? Could somebody point me in the right direction?
# models.py
class Category(models.Model):
name = models.CharField(max_length=30, unique=True)
user = models.ForeignKey(User, blank=True, null=True)
class Meta:
verbose_name_plural = "Ingredience Categories"
def __unicode__(self):
return self.name
# forms.py
class CategoryForm(ModelForm):
class Meta:
model = Category
fields = ('name',)
# views.py
def home(request):
if request.method == 'POST':
catform = CategoryForm(request.POST)
catformInstance = catform.save(commit = False)
catformInstance.save()
return HttpResponseRedirect('')
else:
catform = CategoryForm()
context = {'catform': catform}
return render_to_response('home.html', context, context_instance=RequestContext(request))
# home.html template
<h3>Insert new Category</h3>
<form action="/" method="post" id="ingr-cat-form">{% csrf_token %}
{{ catform.as_p }}
<input type="submit" name="ingrCatForm" value="Save" />
</form>
UPDATE: to clarify, I want to allow user to insert several categories within one form. I think I'm getting close, here is my new version of views.py but it still stores just one category (the last one in the list):
def home(request):
if request.method == 'POST':
catform = CategoryForm(request.POST)
names = request.POST.getlist('name')
catformInstance = catform.save(commit = False)
for name in names:
catformInstance.name = name
catformInstance.save()
return HttpResponseRedirect('')
else:
catform = CategoryForm()
context = {'catform': catform}
return render_to_response('home.html', context, context_instance=RequestContext(request))
You cannot have fields with the same name (on the same Model). If you only need to change the html label in the html form, use
class Category(models.Model):
name = models.CharField(max_length=30, unique=True)
name2 = models.CharField(max_length=30, unique=True, verbose_name="name")
user = models.ForeignKey(User, blank=True, null=True)
or
class CategoryForm(ModelForm):
def __init__(self , *args, **kwargs):
super(CategoryForm, self).__init__(*args, **kwargs)
self.fields['name2'].label = "name"
Here is a working solution. Thanks to #YardenST for pointing me in the right direction. I managed to solve my initial problem by following this tutorial.
# models.py
class Category(models.Model):
name = models.CharField(max_length=30, unique=True)
user = models.ForeignKey(User, blank=True, null=True)
class Meta:
verbose_name_plural = "Ingredience Categories"
def __unicode__(self):
return self.name
# forms.py
class CategoryForm(ModelForm):
class Meta:
model = Category
fields = ('name',)
# views.py
def home(request):
if request.method == 'POST':
catforms = [CategoryForm(request.POST, prefix=str(x), instance=Category()) for x in range(0,3)]
if all([cf.is_valid() for cf in catforms]):
for cf in catforms:
catformInstance = cf.save(commit = False)
catformInstance.save()
return HttpResponseRedirect('')
else:
catform = [CategoryForm(prefix=str(x), instance=Category()) for x in range(0,3)]
context = {'catform': catform}
return render_to_response('home.html', context, context_instance=RequestContext(request))
# home.html template
<h3>Insert new Category</h3>
<form action="/" method="post" id="ingr-cat-form">{% csrf_token %}
{% for catform_instance in catform %} {{ catform_instance.as_p }} {% endfor %}
<input type="submit" name="ingrCatForm" value="Save" />
</form>