Home > Back-end >  how to create django model by pressing button
how to create django model by pressing button

Time:10-26

I am fairly new django user a few months of project creation and messing around with it, and I have created models before using forms, but this would be my first time trying to create one using just a button, and I have no idea how to do that, I looked for examples on stackOverFlow and I found a few but I couldn't figure out how to apply them to my project

I have a products model that uses a category model

class Category(models.Model):

class Meta:
    verbose_name_plural = 'Categories'

name = models.CharField(max_length=254)
friendly_name = models.CharField(max_length=254, null=True, blank=True)

def __str__(self):
    return self.name

def get_friendly_name(self):
    return self.friendly_name


class Product(models.Model):
category = models.ForeignKey('Category', null=True, blank=True, on_delete=models.SET_NULL)
sku = models.CharField(max_length=254, null=True, blank=True)
name = models.CharField(max_length=254)
description = models.TextField()
has_tiers = models.BooleanField(default=False, null=True, blank=True)
price = models.DecimalField(max_digits=6, decimal_places=2)
image_url = models.URLField(max_length=1024, null=True, blank=True)
image = models.ImageField(null=True, blank=True)
favorites = models.ManyToManyField(
    User, related_name='product_favorites', blank=True)

def __str__(self):
    return self.name

and I have a favorite model

class Favorite(models.Model):
"""
A Favorite model so users can save products as their favorites
"""
user = models.OneToOneField(User, on_delete=models.CASCADE)
product = models.ForeignKey(Product, null=False, blank=False, on_delete=models.CASCADE)

I want that when a user is on the products_details.html page, which is just a normal page with the product details displayed nothing major, the user could click a button that says "add to favorite" and that button would go to the url, which would go to the needed view, I know how to do all that, what I don't know is how to create that view code that adds the Product and the User who clicked the button to Favorite model, thats where I hope to get some assistance

as mentioned I looked around on stackOverFlow, and I found a few questions, but couldn't figure out how to solve my problem, I think I found a work around, but I would still like to learn this way of creating models with just pressing a button instead of filling out a form, like I have before

EDIT: needed to change the user field in my favorite model from OneToOneField to a foreign key to make it work

CodePudding user response:

Here's a basic example of how you'd set up something like that.
I used a <a> / Link in the template block, but you've just got to goto the URL that url template tag generates.

Urls.py

urlpatterns = [
    path('add_to_favorites/<int:product_id>', views.add_to_favorites, name='add_to_favorites'),
]

View.py (super basic)

def add_to_favorites(request, product_id):
    # Current user can *always* be grabbed in the view with `request.user` 
    #   Note: May run into issues if the user is not logged in

    Favorite.objects.get_or_create(product_id=product_id, user=request.user)

    # maybe do a redirect, or render different page

Template Button

<a  href="{% url 'add_to_favorites' product_id=product.pk %}">Add to Favorite</a>

<!--
    This is the URL you need: 

    {% url 'add_to_favorites' product_id=product.pk %}

    'product' being the Var you threw the Product Object in to render the Details
--->

If you want it seamless, it might be a good idea to look into Javascript/Jquery & AJAX, and just POST to that URL

  • Related