Ivan
Hi, gays! Excuse me for my english, i just want to ask a question. Anyone have experience in Laravel? Anything like laravel mix exists in django?
Z.R.K
I've got a simple Model like this:
class Product(models.Model):
name = models.CharField(max_length=100)
date_of_sale = models.DateTimeField()
num_ratings = models.IntegerField()
price = models.IntegerField()
And to output a day-by-day breakdown of:
- How many average sales and average rating there were in a day
I made next query:
Product.objects.annotate(day=TruncDay('date_of_sale').\
values('day'). \
annotate(
avg_stars=('num_ratings'),
avg_price=('price')
).order_by('day')
Now I need to get the same data but in intervals:
- morning 8am-12am
- afternoon с 12am-6pm
- evening 6pm-23pm
Expected result:
stats_per_day_part = [
{
'day': '01.01.2019',
'morning': {
'avg_stars': 3.4,
'avg_price': 350
},
'afternoon': {
'avg_stars': 3.4,
'avg_price': 350
},
'evening': {
'avg_stars': 3.4,
'avg_price': 350
}
},
...
]
Any help would be appreciated.
Thanks.
Raudin
Hello everyone, I am working with a form in a 'CreateView' view, I have the success_url property in the view to redirect me to a ListView when submitting in the form, but it only works in a DeleteView, what do I need to do to make it work?
PD: I'm new in django
Manish
I am using custom authentication class and i have some function on that class and now i want to call those functions from api view class. I have already set authentication_classes=[mycustomauthenticationclass,] in APIView class.
I am using DRF.
Help is really appreciated 🙂
Opeyemi
I have a custom user model, and I am adding a slug field to the model. T problem is I don't know how it should be handled in the user manager.
Note: I use signals to create the data for the slug
model.py
class UserManager(BaseUserManager):
def _create_user(self, email, fullname, password, is_staff, is_superuser, **extra_fields):
if not email:
raise ValueError('Users must have an email address')
now = timezone.now()
email = self.normalize_email(email)
fullname = fullname
user = self.model(
email=email,
fullname=fullname,
is_staff=is_staff,
is_active=True,
is_superuser=is_superuser,
last_login=now,
date_joined=now,
**extra_fields
)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, fullname, password, **extra_fields):
return self._create_user(email, fullname, password, False, False, **extra_fields)
def create_superuser(self, email, fullname, password, **extra_fields):
user=self._create_user(email, fullname, password, True, True, **extra_fields)
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
username = None
email = models.EmailField(max_length=254, unique=True)
fullname = models.CharField(max_length=250)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
last_login = models.DateTimeField(null=True, blank=True)
date_joined = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(max_length=255, unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['fullname']
objects = UserManager()
def __str__(self):
return self.email
signals.py
@receiver(pre_save, sender=settings.AUTH_USER_MODEL)
def add_slug_to_user(sender, instance, *args, **kwargs):
if instance and not instance.slug:
slug = slugify(instance.fullname)
random_string = generate_user_string()
instance.slug = slug + "-" + random_string
the problem is how do I handle the slug field in the BaseUserManager if I am using signals to get it created, the methods I try out I get errors, like
TypeError: create_superuser() missing 1 required positional argument: 'slug'