I'm stack on it, please need your help.
I have two models and a want to select all (or filtered) fields from Channel and left join last_published video from Video model. Need good solution using django ORM.
class Channel(models.Model):
name = models.CharField(max_length=150, blank=False, null=False)
url = models.CharField(max_length=100, blank=False, null=False)
def __str__(self):
return self.name
class Video(models.Model):
channel = models.ForeignKey(Channel, on_delete=models.CASCADE)
title = models.CharField(max_length=150, blank=False, null=False)
url = models.CharField(max_length=50, blank=False, null=False, unique=True)
publish_date = models.DateTimeField(blank=True, null=True, auto_created=False)
embedurl = models.CharField(max_length=50, blank=True, null=True, unique=True)
def __str__(self):
return self.title
Related
I have two models: 1) SchoolProfile & 2) Content
I want to show content data in schoolprofile, But data should be display as per user foreignkey.
User foreignkey common for both the models.
School Profile Model
class SchoolProfile(models.Model):
id=models.AutoField(primary_key=True)
user=models.ForeignKey(User,on_delete=models.CASCADE,unique=True)
school_name = models.CharField(max_length=255)
school_logo=models.FileField(upload_to='media/', blank=True)
incharge_name = models.CharField(max_length=255, blank=True)
email = models.EmailField(max_length=255, blank=True)
phone_num = models.CharField(max_length=255, blank=True)
num_of_alu = models.CharField(max_length=255, blank=True)
num_of_student = models.CharField(max_length=255, blank=True)
num_of_cal_student = models.CharField(max_length=255, blank=True)
def __str__(self):
return self.school_name
Content Model
class Content(models.Model):
id=models.AutoField(primary_key=True)
user=models.ForeignKey(User,on_delete=models.CASCADE)
content_type = models.CharField(max_length=255, blank=True, null=True)
show=models.ForeignKey(Show,on_delete=models.CASCADE, blank=True, null=True)
sponsor_link=models.CharField(max_length=255, blank=True, null=True)
status=models.BooleanField(default=False, blank=True, null=True)
added_on=models.DateTimeField(auto_now_add=True)
content_file=models.FileField(upload_to='media/', blank=True, null=True)
title = models.CharField(max_length=255)
subtitle = models.CharField(max_length=255, blank=True, null=True)
description = models.CharField(max_length=500, blank=True, null=True)
draft = models.BooleanField(default=False)
publish_now = models.CharField(max_length=255, blank=True, null=True)
schedule_release = models.DateField(null=True, blank=True)
expiration = models.DateField(null=True, blank=True)
tag = models.ManyToManyField(Tag, null=True, blank=True)
topic = models.ManyToManyField(Topic, null=True, blank=True)
category = models.ManyToManyField(Categorie, null=True, blank=True)
def __str__(self):
return self.title
I have used Inline but it's show below error:
<class 'colorcast_app.admin.SchoolProfileInline'>: (admin.E202) 'colorcast_app.SchoolProfile' has no ForeignKey to 'colorcast_app.SchoolProfile'.
My admin.py
class SchoolProfileInline(InlineActionsMixin, admin.TabularInline):
model = SchoolProfile
inline_actions = []
def has_add_permission(self, request, obj=None):
return False
class SchoolProfileAdmin(InlineActionsModelAdminMixin,
admin.ModelAdmin):
inlines = [SchoolProfileInline]
list_display = ('school_name',)
admin.site.register(SchoolProfile, SchoolProfileAdmin)
Using the StackedInline to link two models is straight forward and a much clean practice, so basically this is my solution below:
class ContentInlineAdmin(admin.StackedInline):
model = Content
#admin.register(SchoolProfile)
class SchoolProfileAdmin(admin.ModelAdmin):
list_display = ('school_name',)
inlines = [ContentInlineAdmin]
enter image description here
I'm having trouble solving this portion of my code.
This is the model.py inside my app.
The "image = models.ImageField(null=True, blank=True)" keeps giving me an error. I install Pillow library but didn't solve the problem
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Customer(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=200, null=True)
price = models.FloatField()
digital = models.BooleanField(default=False,null=True, blank=False)
image = models.ImageField(null=True, blank=True)
def __str__(self):
return self.name
class Order(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True)
date_ordered = models.DateTimeField(auto_now_add=True)
complete = models.BooleanField(default=False)
transaction_id = models.CharField(max_length=100, null=True, blank=False)
def __str__(self):
return str(self.id)
class OrderItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.SET_NULL, blank=True, null=True)
order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
quantity = models.IntegerField(default=0, blank=True, null=True)
date_added = models.DateTimeField(auto_now_add=True)
class ShippingAddress(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True)
order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
address = models.CharField(max_length=200, null=True)
city = models.CharField(max_length=200, null=True)
state = models.CharField(max_length=200, null=True)
zipcode = models.CharField(max_length=200, null=True)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.address
This type of error could mean that you have mixed spaces and tabs in you indentation, or other whitespace issues.
Is the line below it (line 19) empty or does in have some whitespace?
I am new to Django and I need data from two models and would like to do it with one query. Here is what I have in sql that gives me exactly what I need. Can someone please show me how to do it in Django.
select api_userprofile.last_name, api_userprofile.first_name,
api_userdevice.is_admin,
api_userdevice.is_alerts_enabled,api_userdevice.is_owner from
api_userprofile
join api_userdevice on api_userdevice.user_id=api_userprofile.user_id
where api_userdevice.user_id=10 and api_userdevice.device_id=29
These are my 2 models:
class UserDevice(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, null=False)
device = models.ForeignKey(Device, on_delete=models.PROTECT, null=False)
activation_date = models.DateTimeField(default=timezone.now, null=False)
friendly_name = models.CharField(max_length=20, null=True, blank=True)
is_owner = models.BooleanField(null=False, default=False)
is_admin = models.BooleanField(null=False, default=True)
is_alerts_enabled = models.BooleanField(null=False, default=True)
class UserProfile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, null=False)
token = models.TextField(null=False, blank=True)
first_name = models.TextField(null=True, blank=True)
last_name = models.TextField(null=True, blank=True)
Any help would be greatly appreciated.
There is easy way to do that:
User.objects.filter(userdevice__id=29, id=10).values('userdevice__is_owner', 'userdevice__is_admin', 'userdevice__is_alerts_enabled', 'userprofile__first_name', 'userprofile__last_name')
I have the following models.
class Category(MPTTModel):
name = models.CharField(max_length=100, null=False, blank=False)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
def __unicode__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=250, null=False, blank=False)
category = models.ForeignKey('Category')
price = models.DecimalField(decimal_places=2, max_digits=7)
def __unicode__(self):
return self.name
class ProductAttribute(models.Model):
products = models.ManyToManyField('Product')
category = models.ForeignKey('Category')
property = models.CharField(max_length=250, null=False, blank=False)
value = models.CharField(max_length=250, null=False, blank=False)
I need to be able to select products based on a given category, and a given attribute.
For example, if I have the product "Blender", I want to select all blenders within a given category, with a given attribute (such as brand = black & decker).
I have a simple model:
class MediaLink(models.Model):
title = models.CharField(max_length=200)
subtitle = models.TextField(max_length=2000, unique=False, blank=True)
byline = models.CharField(max_length=200, unique=False, blank=True)
url = models.URLField(unique=False)
source = models.CharField(max_length=30, unique=False)
source_url = models.CharField(max_length=30, unique=False, blank=True, null=True, choices=SURL_CHOICES)
mediatype = models.CharField(max_length=30, choices=MEDIATYPE_CHOICES)
topic = models.CharField(max_length=30, choices=TOPIC_CHOICES)
sourceinfo = models.ForeignKey(SourceInfo, blank=True, null=True)
date_added = models.DateField(auto_now_add=True)
def __unicode__(self):
return u'%s' % self.title
class Meta:
abstract = True
class Admin:
pass
I'd like to set the "subtitle" field so that in each object, its initial data is "<h3></h3>". I'm using markdown and having the tags set initially saves me from having to create them in the admin for each record.
You could just set the default on the field:
subtitle = models.TextField(max_length=2000, unique=False, blank=True, default='<h3></h3>')
Or if you're using a ModelForm you can set it in the initial kwarg.