Python
channel too. Are you new to Django?
My models:
class Clients(models.Model):
id_client = models.BigIntegerField(primary_key=True, blank=True)
...
class Hosts(models.Model):
id_host = models.BigIntegerField(primary_key=True)
id_client = models.ForeignKey('Clients', models.DO_NOTHING, db_column='id_client', related_name='host_client_id')
...
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class Registrationform(UserCreationForm):
email = forms.EmailField(required = True)
class Meta:
model =User
fields =(
'username',
'first_name',
'last_name',
'email',
'password1',
'password2'
)
def save(self,commit=True):
user=super(RegistrationForm,self).save(commit=False)
user.first_name=self.cleaned_data['first_name']
user.last_name=self.cleaned_data['last_name']
user.email=self.clelaned_data['email']
if commit:
user.save()
return user
reverse(‘weather’, kwargs={‘current_location’: some_value, 'booking_ location’: another_value})
I am getting a NoReverseMatch exception.
urls.py
and views.py
media
url?
I am using Django Rest Framework and have a model with a timestamp field that I want to group by. The outcome I would like is to have something like {tournament_count: 2, start_date: datetime object, tournaments: [{...tournaments model representations}]}
I currently have a queryset returning the tournament_count and start_date but now I want to drop in the actual tournaments as well.
queryset.annotate(
start_date=Trunc("time_stamp", "day", output_field=DateTimeField())
)
.values("start_date")
.annotate(tournament_count=Count("id")).order_by()
I can't figure out a solution that will work well and scale well. Any thoughts?
Hello people, has anyone here managed to create user profiles while using Django allauth for authentication. I am having challenges with user has no profile errors and RelatedObjectDoesNotExist a yet I have a Profile model with OnetoOne relationship with User model and a profile view plus use of Django signals for profile creation and saving
Users/views.py
`views.py
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import ProfileUpdateForm
@login_required
def profile(request):
if request.method == 'POST':
p_form = ProfileUpdateForm(request.POST,
request.FILES,
instance=request.user.profile)
if p_form.is_valid():
p_form.save()
messages.success(request, f'Your account has been updated!')
return redirect('profile')
else:
p_form = ProfileUpdateForm(instance=request.user.profile)
context = {
'p_form': p_form
}
return render(request, 'profile.html', context)
`
MODELS.PY
`
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(default='default.jpg', upload_to='profile_pics')
role = models.CharField(max_length=25,choices=role, default='Freelancer')
def __str__(self):
return f'{self.user.username} Profile'
`
SIGNALS.PY
`
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
profile.save()
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
`