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()
`
Does anyone know how to query another model based on Foreign Key? Say I have a Project model that is a FK in task model and I want to generate all the task_names by querying the Task model and only see tasks applicable to a specific project?
Tried doing this and it is not working project_tasks=Task.objects.filter(project_id__in=Project.objects.all()).order_by('latest_submission_time')
qamarali
This link would help you outqamarali
I think you want to export user data from model to PDF,qamarali
This link will definitely help you out...I have a dictionary which contains one item (“current_location”, which is a nested dict) and I would like to access that nested dict. However, I cannot use the key as the code will break if a different key is passed, e.g. “different_location".
How can I access the first item in a dictionary without using a key? The dict looks like this:
{'current_location': {'date': '2020-01-27T10:28:24.148Z', 'type_icon': 'partly-cloudy-day', 'description': 'Mostly Cloudy', 'temperature': 68.28, 'wind': {'speed': 10.48, 'bearing': 178, 'gust': 12.47}, 'rain_prob': 0.02, 'latitude': '-33.927407', 'longitude': '18.415747', 'request_id': 31364, 'request_location': 'Current location'}}
I have these two models
class Project(models.Model):
project_name = models.CharField(max_length=100, blank=False)
description = models.TextField(max_length=500, default=None)
postedOn = models.DateTimeField(auto_now_add=True, blank=True)
Owner = models.ForeignKey(User, on_delete=models.CASCADE)
isCompleted = models.BooleanField(default=False)
deadline = models.DateTimeField(blank=True, null=True)
task_count = models.IntegerField(default=0)
attachments=models.FileField(default='Project Attachments', upload_to='tasks_docs', validators=[validate_file_extension])
def __str__(self):
return self.project_name
class Task(models.Model):
task_name = models.CharField(max_length=50, blank=False)
addedOn = models.DateTimeField(auto_now_add=True, blank=True)
project = models.ForeignKey(Project, on_delete=models.CASCADE, default=1)
rating = models.DecimalField(default=0, max_digits=2, decimal_places=1)
amount = models.IntegerField(default=0)
task_description = models.TextField(max_length=1000, default=None)
task_link = models.URLField(blank=True)
latest_submission_time = models.DateTimeField(blank=True, null=True)
isCompleted = models.BooleanField(default=False)
deadline = models.DateTimeField(blank=True, null=True)
task_attachments=models.FileField(default='Tasks To Do', upload_to='tasks_docs', validators=[validate_file_extension])
def __str__(self):
return self.task_name
And I am looking for a way to access tasks based on project. I can select a specific project and view its tasks