how to update a model data from the admin panel in django - django

so i was creating a ecommerce site , what i wanted to do was when i added a product through admin panel , it would increase the count as well from model.
eg:
in the below model ,when i add an a item from admin panel i want it to find the object and add the objects stock with the new stock number i provided , if both these added items have the same names.
class Item(models.Model):
name = models.CharField(max_length=100)
price = models.FloatField()
product = models.ForeignKey(Product, on_delete=models.CASCADE)
desc = models.TextField()
img = models.ImageField(upload_to='uploads/items', blank=True)
stock = models.IntegerField()
def __str__(self):
return self.name

Simply use Update View available in Django.
Here is an official Documentation. Link

Related

Merge Django models into a view

I am attempting to merge and pull data from three Django models into a view. Players and Events relate to each other in the Details model (a player can attend many events) using models.ForeignKey.
In other platforms I would have written a DB View to join tables and the application would query that view.
From what I understand Django does not support data views within Models.
Looking for help on how I would approach this in Django.
class Players(models.Model):
firstName = models.CharField(max_length=255)
lastName = models.CharField(max_length=255)
highSchool = models.CharField(max_length=255)
gradYear = models.IntegerField()
slug = models.SlugField(default="", null=False)
class Events(models.Model):
title = models.CharField(max_length=255)
location = models.CharField(max_length=255)
date = models.DateField()
class Details(models.Model):
event = models.ForeignKey(Events, on_delete=models.CASCADE)
player = models.ForeignKey(Players, on_delete=models.CASCADE)
height = models.IntegerField(default=None, blank=True)
weight = models.IntegerField(default=None, blank=True)
def playerdetail(request,slug):
playerinfo = Details.objects.get(id=1)
template = loader.get_template('playerdetail.html')
context = {
'playerinfo': playerinfo,
}
return HttpResponse(template.render(context, request))
You are actually doing what you needed to do with the code you provided.
When you are invoking a query on a model that connects those two entities (Players,Events), it performs the join when you try to access each of these properties through the foreign key fields.
For example, for accessing the player information (which makes the Django ORM perform the join operation in the background):
# Get the first name of the player
first_name = playerinfo.player.firstName
For filtering and showing in other places, you can use the notation field__subfield
For more information, please read the examples of this website:
https://books.agiliq.com/projects/django-orm-cookbook/en/latest/index.html

Django admin: reverse select foreign keys

I want to add pre-existing foreignkey items from the parent's admin. I have a "Bundle" model to hold "Product" items; many-to-one/foreignkey:
models.py
class Bundle(models.Model):
title = models.CharField(max_length=300)
class Product(models.Model):
title = models.CharField(max_length=300)
bundle = models.ForeignKey(
'Bundle',
on_delete=models.CASCADE,
null=True,
blank=True,
)
Below, I used StackedInline but this is for creating new products with a form:
admin.py
class ProductInline(admin.StackedInline):
model = Product
#admin.register(Bundle)
class BundleAdmin(admin.ModelAdmin):
inlines = [
ProductInline,
]
Instead, I want to repeatedly add existing products from a dropdown/search in the Bundle section of admin. So, I make a Bundle and then add a series of Products from a dropdown / with a search.
Thanks in advance.
For you requirement, you can use ManyToManyField in Bundle model instead of ForeignKey in Product model.
Check below code.
class Bundle(models.Model):
title = models.CharField(max_length=255)
product = models.ManyToManyField('Product')
class Product(models.Model):
title = models.CharField(max_length=255)
Then you can register admin interfaces:
admin.site.register(Bundle)
admin.site.register(Product)
Then you can add series of Product from a dropdown/search.

How to add multiple model objects on the same admin page Django

The idea is super simple. This is specific to my project, I want to add data to 2 objects of the same model on one Django admin page and have one save button.
My class:
class Data(models.Model):
issuer = models.ForeignKey(Issuers, on_delete=models.CASCADE)
issuer_field = models.ForeignKey(Fields, on_delete=models.CASCADE, null=True)
data_year = models.PositiveSmallIntegerField()
long_term_ar = models.IntegerField()
section_1 = models.IntegerField()
short_term_ar = models.IntegerField()
short_term_investments = models.IntegerField()
cash = models.IntegerField()
To illustrate:I want to have 2 of those on the same page
I am new to Django, and it will be super helpful if you will help me implement this idea
Take a look on "Inlinemodeladmin objects": https://docs.djangoproject.com/en/2.2/ref/contrib/admin/#inlinemodeladmin-objects
You will be able to add more than one record at the same time:
Try this approach. I am still learning Django too. However, I believe that you dont need to specify the save instance since it is already built in django.
models.py
class Data(models.Model):
issuer = models.ForeignKey(Issuers, on_delete=models.CASCADE)
issuer_field = models.ForeignKey(Fields, on_delete=models.CASCADE, null=True)
data_year = models.PositiveSmallIntegerField()
long_term_ar = models.IntegerField()
section_1 = models.IntegerField()
short_term_ar = models.IntegerField()
short_term_investments = models.IntegerField()
cash = models.IntegerField()
admin.py
from django.contrib import admin
from .models import Data
class DataAdmin(admin.ModelAdmin):
list_display = ('issuer', 'issuer_field')
admin.site.register(Data)

Creating list of photos in Django

On the webpage I am currently working on I have to display profiles of different persons. Each person has a profile image that is displayed on the right side. Furthermore, I have the content saved as markdown on the server.
Now I want to add the possibility to add an optional list of images that are displayed under the text and shows something about the person. My model for the person looks like that:
class stipendiat(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
content = models.TextField()
year = models.IntegerField(blank=False, null=False)
form = models.IntegerField(blank=False, null=False)
image = models.ImageField(upload_to='stipendiaten/', blank=True, null=True, default='default.jpg')
gallery = # This is the question
def __unicode__(self):
return self.first_name + " " + self.last_name
Because I want to save some data about the image the model should look like that:
class Image(models.Model):
caption = models.CharField(max_length=512)
year = models.IntegerField()
image = models.ImageField(upload_to=upload_path_handler, blank=False, null=False)
For convenience it should be possible to add an image while editing the form for the person. To be precise, I want to add a person and fill in the necessary information (first_name, last_name, content etc.). At the end should be a list of connected images and a button to add an image rapidly.
Is it necessary to use an external app or is there an elegant solution with relation fields?
Maybe you should use a different model for the gallery images and a foreign key field to link the two models together
class Image(models.Model):
#...
stipendiat = models.ForeignKey(Reporter)
Now you can retrieve the Gallery Images for a stipendiat with id 1 like this
Image.objects.filter(stipentiat__pk=1)

Title field in admin

I have a model:
class Review(models.Model):
name = models.CharField(max_length = 50)
link = models.CharField(max_length = 100)
book_id = models.IntegerField()
review_id = models.IntegerField()
content = models.TextField()
rating = models.IntegerField()
author = models.CharField(max_length = 50)
If I open admin http://127.0.0.1:8000/admin/my_app/review/ I'll see the list of records. For each record Django Admin display only one field ('Name' in my case). How Django Admin choise the field to display in the list of records.
Firstly I thinked that it is a first field in my model. I have moved Name-field down in the field list and recreate the database, but there was nothing to change.
Django calls the __unicode__ method on the model. For configuring the displayed fields in the change list see the detailed documentation!