Django: cannot register model in admin panel
django admin inline foreign key
admin.site.register not working
django admin custom form
django admin permissions
django admin custom field
django model field not showing in admin
I've various models in my apps. However, there is this one that I cannot register so I can see it in the
So in my
admin.py file, I can use:
from django.contrib import admin from .models import Cart, CartItem # Register your models here. admin.site.register(Cart)
from django.contrib import admin from .models import Cart, CartItem # Register your models here. admin.site.register(Cart, CartItem)
Because I get this error:
File "/home/ogonzales/Escritorio/projects_envs/perfectcushion_env/lib/python3.6/site-packages/django/contrib/admin/checks.py", line 26, in check_admin_app errors.extend(site.check(app_configs)) File "/home/ogonzales/Escritorio/projects_envs/perfectcushion_env/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 81, in check if modeladmin.model._meta.app_config in app_configs: AttributeError: 'CartItem' object has no attribute 'model'
from django.db import models from shop.models import Product # Create your models here. class Cart(models.Model): cart_id = models.CharField(max_length=250, blank=True) date_added = models.DateField(auto_now_add=True) class Meta: db_table = 'Cart' ordering = ['date_added'] def __str__(self): return self.cart_id class CartItem(models.Model): product = models.ForeignKey(Product, on_delete=models.CASCADE) cart = models.ForeignKey(Cart, on_delete=models.CASCADE) quantity = models.IntegerField() active = models.BooleanField(default=True) class Meta: db_table = 'CartItem' def sub_total(self): return self.product.price * self.quantity def __str__(self): return self.product
from django.shortcuts import render, redirect, get_object_or_404 from shop.models import Product from .models import Cart, CartItem from django.core.exceptions import ObjectDoesNotExist import stripe from django.conf import settings from order.models import Order, OrderItem # Create your views here. def _cart_id(request): cart = request.session.session_key if not cart: request.session.create() # it does not return anything. that is why `cart = request.session.create()` will not work cart = request.session.session_key return cart # Ultimately return cart def add_cart(request, product_id): product = Product.objects.get(id = product_id) try: cart = Cart.objects.get(cart_id=_cart_id(request)) except Cart.DoesNotExist: cart = Cart.objects.create( cart_id = _cart_id(request) ) cart.save() try: cart_item = CartItem.objects.get(product = product, cart = cart) if cart_item.quantity < cart_item.product.stock: cart_item.quantity += 1 cart_item.save() except CartItem.DoesNotExist: cart_item = CartItem.objects.create( product = product, quantity= 1, cart = cart, ) cart_item.save() return redirect('cart:cart_detail') def cart_detail(request, total = 0, counter = 0, cart_items = None): try: cart = Cart.objects.get(cart_id = _cart_id(request)) cart_items = CartItem.objects.filter(cart = cart, active=True) for cart_item in cart_items: total += (cart_item.product.price * cart_item.quantity) counter += cart_item.quantity except ObjectDoesNotExist: pass stripe.api_key = settings.STRIPE_SECRET_KEY stripe_total = int(total * 100) description = 'Perfect Cushion Shop - New Order' data_key = settings.STRIPE_PUBLISHABLE_KEY if request.method == "POST": print(request.POST) try: token = request.POST['stripeToken'] email = request.POST['stripeEmail'] billingName = request.POST['stripeBillingName'] billingAddress1 = request.POST['stripeBillingAddressLine1'] billingcity = request.POST['stripeBillingAddressCity'] billingPostcode = request.POST['stripeBillingAddressZip'] billingCountry = request.POST['stripeBillingAddressCountry'] shippingName = request.POST['stripeShippingName'] shippingAddress1 = request.POST['stripeShippingAddressLine1'] shippingcity = request.POST['stripeShippingAddressCity'] shippingPostcode = request.POST['stripeShippingAddressZip'] shippingCountry = request.POST['stripeShippingAddressCountryCode'] customer = stripe.Customer.create( email = email, source = token ) charge = stripe.Charge.create( amount = stripe_total, currency = 'pen', description = description, customer = customer.id ) '''Creating the order''' try: order_details = Order.objects.create( token = token, total = total, emailAddress = email, billingName = billingName, billingAddress1 = billingAddress1, billingCity= billingcity, billingPostCode= billingPostcode, billingCountry = billingCountry, shippingName = shippingName, shippingAddress1 = shippingAddress1, shippingCity=shippingcity, shippingPostcode = shippingPostcode, shippingCountry = shippingCountry ) order_details.save() for order_item in cart_items: oi = OrderItem.objects.create( product = order_item.product.name, quantity = order_item.quantity, price = order_item.product.price, order = order_details ) oi.save() print("La orden fue creada") print("Esta es la orden") print(str(oi)) return redirect('order:thanks', order_details.id) except ObjectDoesNotExist: pass except stripe.error.CardError as e: return False,e return render(request, 'cart.html', dict(cart_items = cart_items, total = total, counter = counter, data_key = data_key, stripe_total = stripe_total, description = description)) def cart_remove(request, product_id): cart = Cart.objects.get(cart_id = _cart_id(request)) product = get_object_or_404(Product, id = product_id) cart_item = CartItem.objects.get(product=product, cart = cart) if cart_item.quantity > 1: cart_item.quantity -= 1 cart_item.save() else: cart_item.delete() return redirect('cart:cart_detail') def full_remove(request, product_id): cart = Cart.objects.get(cart_id = _cart_id(request)) product = get_object_or_404(Product, id = product_id) cart_item = CartItem.objects.get(product=product, cart = cart) cart_item.delete() return redirect('cart:cart_detail')
from django.contrib import admin from .models import Cart, CartItem # Register your models here. admin.site.register(Cart) admin.site.register(CartItem)
model not showing up in django admin, HmmmmTry change include of your app in settings.py: From: INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', from django.apps import apps models = apps.get_models () for model in models: admin.site.register (model) This will fetch all the models in all apps and registers them with the admin interface. This works well if we are automatically registering all the models.
You have to call admin.site.register twice for the two models:
Django: cannot register model in admin panel, Django: cannot register model in admin panel. django admin inline foreign key admin.site.register not working django models django admin custom field django Register the Venue model with the admin; line 5. Register the MyclubUser model with the admin; Line 6. Register the Event model with the admin; Save the file, refresh your browser and your admin index page should now look like Figure 7-3. Note that you now have an Events group under the Authentication and Authorization group.
Or you can do:
The Django admin site | Django documentation, from django.contrib import admin # Register your models here. Unfortunately we can't directly specify the genre field in list_display because it By default, logging in to the admin requires that the user has the is_superuser or is_staff attribute set to True. Finally, determine which of your application’s models should be editable in the admin interface. For each of those models, register them with the admin as described in ModelAdmin.
Django Tutorial Part 4: Django admin site, Superuser login, register and customize models in the admin, add and edit model With Django's admin you can authenticate users, display and handle forms, and Be careful though—you can't hide required fields; otherwise, the form won't save! Scroll down the edit page to the User permissions panel and add the from django.contrib import admin # Register your models here. Register the models by copying the following text into the bottom of the file. This code simply imports the models and then calls admin.site.register to register each of them.
The Django Admin - Python Django Tutorials, Comments. We were unable to load Disqus. If you are a moderator please see our troubleshooting guide. Sam Django Admin is a powerful tool for managing data in your app. However, it was not designed with summary tables and charts in mind. Luckily, the developers of Django Admin made it easy for us to…
Automatically Register All Models In Django Admin, By registering the Question model with admin.site.register(Question) , Django was able to construct a default form Note that you can't remove the original three slots. That's easy to change, though, using Django's template system. blog/admin.py. from django.contrib import admin from.models import Post admin.site.register(Post) As you can see, we import (include) the Post model defined in the previous chapter. To make our model visible on the admin page, we need to register the model with admin.site.register(Post). OK, time to look at our Post model.