Categories
Mastering Development

object has no attribute in Django Rest Framework

I develop a mobile application with Django Rest Framework at behind, and React Native at front side. I have to models and nested serializers. I need to insert record at same time to them. But I have ‘Entity’ object has no attribute ‘automobile’ error. When I check similar examples, I do not understand where I […]

Categories
Mastering Development

Django: How to create a queryset from a model relationship?

I have been struggling with grasping relations for some time and would be very grateful if someone can help me out on this issue. I have a relation that connects the User model to a ProcessInfo model via one to many and then I have a relation that connects the ProcessInfo to the ProcessAssumptions as […]

Categories
Mastering Development

ValueError at /category/sugar/ Field ‘id’ expected a number but got ‘sugar’

I’m trying to make category for eCommerce urlpatterns = [ path(‘category/<cats>/’, views.categoryView , name = ‘category’) ] views.py: def categoryView(request, cats): products_category = Product.objects.filter(category=cats) return render(request, ‘store/categories.html’, {‘cats’: cats.title(), ‘products_category’: products_category}) models.py class Category(models.Model): name = models.CharField(max_length=100) image = models.FileField(null=True, blank=True) def __str__(self): return self.name def get_absolute_url(self): return reverse(‘index’) @property def imageURL(self): try: url = […]

Categories
Mastering Development

“Unique constrainted failed” problem when running migrations

I have been tryed to change a django model by applying and running migrations but I have been getting this error django.db.utils.IntegrityError: UNIQUE constraint failed: new__startup_home_mates.user_id How can this error be fixed? I have already investigated a lot but I have found nothing that fits my situation. I have already tried to use RenameField and […]

Categories
Mastering Development

How to sum OneToMany related records Django

I have two models class Salary(models.Model): date = models.DateField(_(‘date’)) user_id = models.IntegerField(_(‘user_id’)) earned = models.IntegerField(_(‘earned’)) prepayment = models.IntegerField(_(‘prepayment’)) … created_at = models.DateTimeField(_(‘created at’), auto_now_add=True) class WebsitePayment(models.Model): website_id = models.IntegerField(_(‘website_id’)) amount = models.IntegerField(_(‘amount’)) salary = models.ForeignKey(Salary, related_name="payments", on_delete=models.CASCADE, null=False, blank=False) What I need is data contains grouped by date or by user values with sum for […]

Categories
Mastering Development

first_name in django model

how can I access to first_name of a user in django template?. for example my writer in django model is like this below : writer = models.ForeignKey(User, on_delete=models.CASCADE)

Categories
Mastering Development

How to retrieve objects from foreign key relation in model

so I am trying to retrieve my Post object from my *Bookmark model which is based on a base model. However, it seems it is always returning null, and I was wondering how can I retrieve my post oject? This is my bookmark model: class BookmarkBase(models.Model): class Meta: abstract = True user = models.ForeignKey(Profile,on_delete=models.CASCADE, verbose_name="User") […]

Categories
Mastering Development

A URL of a Hyperlinked Model Serializer returns ‘{“detail”: “not found.”} for a model with custom PK

class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True) website = models.URLField(max_length=30, null=True, blank=True) class ProfileViewSet(viewsets.ModelViewSet): lookup_field = ‘user’ serializer_class = ProfileSerializer queryset = Retailer.objects.all() class ProfileSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Profile fields = [‘website’, ‘user’, ‘url’] extra_kwargs = { ‘url’: {‘view_name’: ‘profile-detail’, ‘lookup_field’: ‘user’}, ‘user’: {‘lookup_field’: ‘username’, ‘view_name’: ‘user-detail’} } The code above lists all profiles […]

Categories
Mastering Development

Can’t create object inside migrations

I’m extending django.contrib.auth.User model with my model: class Farmer(models.Model): user = models.OneToOneField(get_user_model(), on_delete=models.CASCADE, related_name=”farmer”) mob_phone = models.CharField(max_length=12) I want to create some of these Farmer instances during migrations, so i have a python-migration job, which goes after all other migrations : def create_farmers(apps, schema_editor): db_alias = schema_editor.connection.alias Farmer = apps.get_model(“farmer”, “Farmer”) User = get_user_model() u […]

Categories
Mastering Development

Django. How to restrict the number of coupon redeemed

Hey guys I am learning to develop a ecommerce website, I have a coupon model with number object, which take care of max number of coupon available to use, I want to implement that in my views, such that if the coupon is redeemed more than the number(number of coupons), then it should show an […]