Categories
Mastering Development

Django: How to filter a subset in a querset?

How can I filter the elements in a subset? class Order(models.Model): user = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL) … class Bill(models.Model): order = models.ForeignKey(Order, blank=True, null=True, on_delete=models.SET_NULL)) billdate = models.DateTimeField(default=timezone.now) payed = models.BooleanField(default=False) … order = Orders.objects.all() order[0].bill_set will return all Bills. But I just want to get the latest 3 elements, ordererd by billdate. How […]

Categories
Mastering Development

How can only certain users see the page?

I created a system with Django. I have several users and these users can have different ranks. I have a page but I want to just one rank (lead) can see these page. How can I do that? models.py class UserProfile(AbstractUser): ranks = ( (‘analyst’, ‘Analyst’), (‘seniorAnalyst’, ‘Senior Analyst’), (‘lead’, ‘Lead’), (‘manager’, ‘Manager’), (‘seniorManager’, ‘Senior […]

Categories
Mastering Development

How to serialize only certain elements of M2M set of the object being serialized?

So I have these models: class Group(models.Model): name = models.CharField(max_length=255, blank=True, null=True, default=None) class Member(models.Model): name = models.CharField(max_length=255, blank=True, null=True, default=None) group = models.ForeignKey(Group, related_name=’members’, on_delete=models.CASCADE) is_deleted = models.BooleanField() And these serializers: class GroupSerializer(serializers.ModelSerializer): members = MemberSerializer(many=True, read_only=True) class Meta: model = Group fields = ‘__all__’ class MemberSerializer(serializers.ModelSerializer): # some field defintions here class Meta: […]

Categories
Mastering Development

how can i collect imgaes from template and save it to database in django

I want users to add blogs with images. So I have to collect images from them through the template and save them to the database. But it’s just collect the name of the images. views.py ”’ def create_post(request): if request.method == ‘POST’: title = request.POST[‘title_id’] catagory = request.POST[‘catagory_id’] #author = request.POST[‘author_id’] description = request.POST[‘description_id’] notes […]

Categories
Mastering Development

Django: reference model fields from another model

I have two models. What i need is to reference the name and the email field from the Users model to the Customer model fields. Is the following way correct? class Users(AbstractBaseUser): name = models.CharField(max_length=200) email = models.CharField(max_length=200) from users.models import Users class Customer(models.Model): user = models.OneToOneField( Users, on_delete=models.CASCADE, null=True, blank=True) name = models.OneToOneField( Users.name, […]

Categories
Mastering Development

MultipleObjectsReturned at /cart/ in Django

i’m a beginner and i am getting this error "Exception Value: get() returned more than one Order — it returned 2!" views.py def cart(request): customer=request.user.customer order,created=Order.objects.get_or_create(customer=customer,complete=False) items=order.orderitem_set.all() context={ ‘items’:items } return render(request, ‘catalog/cart.html’,context) models.py class Order(models.Model): customer=models.ForeignKey(Customer,on_delete=models.SET_NULL, blank=True, null=True) date_orderd=models.DateTimeField(auto_now_add=True) complete=models.BooleanField(default=False, null=True, blank=False) transaction_id=models.CharField(max_length=200, null=True) def __str__(self): return str(self.id) class OrderItem(models.Model): product=models.ForeignKey(Product, on_delete=models.SET_NULL, null=True) order=models.ForeignKey(Order, […]

Categories
Mastering Development

Django Admin – Adding images to form raises an error

Hello there I want to add images to a form from my admin panel with Django. I’ve created the field like so: photo = models.ImageField(null=True, blank=True, upload_to=’images/’) but I get the following error: no such column: blog_post.company Request Method: GET error: 500 151197

Categories
Mastering Development

Django – How to query a list inside of a filter method

I am new to Django and I want to query a list inside of an object. I have a list of Batches and each batch has a BatchComment list. Both of them has a User property. What I want to do is get batches where user has a comment and only get ones where the […]

Categories
Mastering Development

Custom Django SlugField Validation Breaks URL Matching

I wrote a custom field validator for slugs in my URLs (accepting periods in slugs for one model, SLUG_REGEX = ‘[-a-zA-Z0-9_.]+$’): def validate_my_slug(value): my_slug_re = re.compile(settings.SLUG_REGEX) if not my_slug_re.match(value): raise ValidationError( _(‘Slugs must consist of letters, numbers, ‘ ‘underscores, hyphens, or periods.’), params={‘value’: value}, ) The field in models.py: slug = models.CharField(max_length=64, null=True, blank=True, validators=[validate_my_slug]) […]

Categories
Mastering Development

Django + React Able to create new user in database, but token-auth returns 400 bad request

So I have an app that I’m writing. It’s working, for the most part, so far. I only currently have a login and signup setup, with a ‘homepage’ that just has a logout button and some text on it so I can test. When I do signup, it posts the user to the database, but […]