So in my models.py I have this:
class Profesie(models.Model):
titlu = models.CharField(max_length=100)
def __str__(self):
return self.titlu
class Domeniu(models.Model):
profesie = models.ForeignKey(Profesie)
titlu = models.CharField(max_length=100)
def __str__(self):
return self.titlu
class Anunt(models.Model):
titlu = models.CharField(max_length=150)
user = models.ForeignKey('auth.User', related_name='anunturi')
profesie = models.ForeignKey(Profesie)
domeniu = models.ForeignKey(Domeniu)
In the form when the user selects the field profesie, and then domeniu, for the domeniu field it should only display the values that are in the Profesie table. How can I accomplish that?
Related
I am a Django newbie. I have a model list like below. In this example, I have friendlists and friendrequestlists having users, but I want to have users having friendslists and friendrequestlists. How should I modify this code:
from django.db import models
from datetime import datetime
from django.contrib.auth.models import User, Group
class Word(models.Model):
definition = models.CharField(max_length=350)
#add_date = models.DateTimeField(default=str(datetime.now()), editable=False)
turkish = models.CharField(max_length=50)
english = models.CharField(max_length=50)
users = models.ManyToManyField(User)
groups = models.ManyToManyField(Group)
creator = models.CharField(max_length=50)
visibility = models.CharField(max_length=2, default='2') #0(only me), 1(friends), 2(groups), 3(everbody)
def __str__(self):
return self.english
#def add_date_pretty(self):
# return self.add_date
def summary(self):
return self.definition[:50] + "..."
class GroupEx(models.Model):
group = models.OneToOneField(Group)
admins = models.ManyToManyField(User)
def __str__(self):
return self.group.name
class GroupReqList(models.Model):
group = models.OneToOneField(Group)
member_request = models.ManyToManyField(User)
def __str__(self):
return self.group.name
class UserEx(models.Model):
user = models.OneToOneField(User)
casel = models.CharField(max_length=12)
def __str__(self):
return self.user.username
class FriendList(models.Model):
user = models.OneToOneField(UserEx)
friends = models.ManyToManyField(User)
def __str__(self):
return self.user.user.username
class FriendReqList(models.Model):
user = models.OneToOneField(UserEx)
friend_requests = models.ManyToManyField(User)
def __str__(self):
return self.user.user.username
I am using inline model admin and many to many relation. I have two model classes User and Videodata and i linked them with many to many relation. Below is my models
class User(models.Model):
user_id = models.CharField(max_length=40,unique=True)
user_name = models.CharField(max_length=40)
user_email = models.EmailField()
user_city = models.CharField(max_length=40)
videos_watched = models.ManyToManyField('VideoData', through='WatchedVideo')
class Meta:
ordering = ['user_id']
verbose_name = 'User MetaData'
verbose_name_plural = 'Users MetaData'
def __unicode__(self):
return self.user_id
class VideoData(models.Model):
video_id = models.CharField(max_length=40,unique=True)
video_name = models.CharField(max_length=40)
class Meta:
verbose_name = 'User_Video MetaData'
verbose_name_plural = 'Users_Video MetaData'
def __unicode__(self):
return self.video_id
class WatchedVideo(models.Model):
user = models.ForeignKey(User, to_field = 'user_id')
videoData = models.ForeignKey(VideoData, to_field = 'video_id')
time = models.PositiveIntegerField(null = True)
In User table i see video data but with video_id but i want video_name to be displayed there. I am attaching a link of current output.
https://drive.google.com/open?id=0B0JOKpp3abpzOFN3LVg0WEs3a0k
You can see in above pic video id is there but i want video name. How can i do that??
You can display the name changing the method __unicode__ of VideoData to:
def __unicode__(self):
return self.video_name
EDIT: Add method __str__ to VideoData:
def __unicode__(self):
return self.video_id
def __str__(self):
return self.video_name
I displayed names with following change:
def __unicode__(self):
return self.video_name
def __str__(self):
return self.video_id
I have to create a view which returns me the posts of a particular owner/author
This error in coming from view bellow. I know this is a simple thing that I am missing. Please help me on this: (marlm is the ID I created to test)
def view_by_owner(request):
user = request.user.username
posts_owner = Post.objects.filter(owner=user)
return render_to_response('view_post.html',{'view_owner':posts_owner})
Models:
from django.db import models
from django.db.models import permalink
from django.contrib.auth.models import User
class Post(models.Model):
title = models.CharField(max_length=100)
body = models.TextField()
datposted = models.DateTimeField('date posted')
category = models.ForeignKey('Category')
owner = models.ForeignKey('UserProfile')
def __str__(self):
return '%s' % self.title
class Category(models.Model):
title = models.CharField(max_length=100)
def __str__(self):
return self.title
class UserProfile(models.Model):
# This line is required. Links UserProfile to a User model instance.
user = models.OneToOneField(User)
# The additional attributes we wish to include.
website = models.URLField(blank=True)
picture = models.ImageField(upload_to='profile_images', null=True)
# Override the __unicode__() method to return out something
def __unicode__(self):
return self.user.username
class Logout(User):
force_logout_date = models.DateTimeField(null=True, blank=True)
The problem is that owner is a foreign key to User, not the value of the user name. To lookup by that, you will need to span that relationship, like so:
posts_owner = Post.objects.filter(owner__user__username=user)
Or, more simply, you could do this:
posts_owner = Post.objects.filter(owner__user=request.user)
I just start to learn Django and I want to create a Product model with attributes, custom fields and custom field options. Custom field options exemple:
Line 1: [YOUR TEXT] | Custom field options: [FONT] [FONT SIZE] [...]
Line 2: [YOUR TEXT] | Custom field options: [FONT] [FONT SIZE] [...]
So I've created this models:
from django.db import models
from django.utils import timezone
class Product(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
sku = models.CharField(max_length=100)
slug = models.CharField(max_length=100)
price = models.DecimalField(max_digits=6, decimal_places=2)
active = models.BooleanField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.name
class ProductMeta(models.Model):
product = models.OneToOneField('Product')
title = models.CharField(max_length=100)
description = models.TextField(max_length=250)
class ProductImage(models.Model):
def upload_path(self, filename):
return 'static/uploads/images/%s%s' % (timezone.now().strftime('%Y/%m/%d/%Y%m%d_'), filename)
product = models.ForeignKey('Product')
name = models.CharField(max_length=100)
default = models.BooleanField()
image = models.ImageField(upload_to=upload_path)
def __unicode__(self):
return self.name
class ProductCharacteristic(models.Model):
product = models.ForeignKey('Product', related_name="characteristics")
name = models.CharField(max_length=100)
value = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class ProductAttribute(models.Model):
category = models.ForeignKey('ProductAttributeCategory')
products = models.ManyToManyField('Product', related_name="attributes")
name = models.CharField(max_length=100)
ordering = ['-category']
def __unicode__(self):
return u'%s : %s' % (self.category, self.name)
class ProductAttributeCategory(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class ProductAttributeValue(models.Model):
attribute = models.ForeignKey('ProductAttribute', related_name="values")
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class ProductCustomField(models.Model):
product = models.ForeignKey('Product', related_name="custom_fields")
name = models.CharField(max_length=100)
description = models.TextField(max_length=250)
def __unicode__(self):
return self.name
class ProductCustomFieldOption(models.Model):
fields = models.ManyToManyField('ProductCustomField', related_name="options")
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class ProductCustomFieldOptionValue(models.Model):
option = models.ForeignKey('ProductCustomFieldOption', related_name="values")
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
But now I don't know how to create the form in product details page in which the user can choose the product attributes (color, size...) and the product custom fields (and custom fields options). I've tried a lot of things but no results.
Can you help me please? :)
your question is unclear to me and your even more confusing. However see this if it helps
In your models.py
from django.db import models
from model_utils import Choices
colour_choices = ('Blue', 'Green')
class Product(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
reuturn self.name
class ProductAttributes(models.Model):
product = models.Foreignkey(Product, related_name='products')
colour = models.CharField(choices=Choices(*colour_choices))
In your forms.py
from django import forms
from .models import Product, ProductAttributes
class ProductForm(forms.ModelForm):
class Meta:
model = Product
class ProdductAttributesForm(forms.ModelForm):
class Meta:
model = ProductAttributes
Write your views.py, urls.py and template accordingly
this method will give you a text box to enter products and drop-down for choosing color.
Hope it helped!
I am doing a Django application and I have two models for my stock app.
class Watchlist(models.Model):
def number():
no = Watchlist.objects.count()
if no == None:
return 1
else:
return no + 1
name = models.CharField(max_length=200)
created = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey('users.User')
stocks = models.ManyToManyField('Stock')
equityboss = models.BooleanField(blank=True, default=False)
last_watched = models.DateTimeField(auto_now_add=True)
order = models.IntegerField(max_length=10, default=number)
def __unicode__(self):
return "%s - %s"%(self.name, self.id)
class Meta:
unique_together = ("name", "user")
def as_json(self):
return dict(
name=self.name,
created=self.created.isoformat(),
equityboss=self.equityboss,
id = self.id,
last_watched=self.last_watched.isoformat(),
order=self.order)
class IndicatorPreference(models.Model):
User = models.ForeignKey('users.User')
indicator = models.ForeignKey('Indicator')
visible = models.BooleanField(default=True)
order = models.IntegerField(max_length=2, db_index=True)
Watchlist = models.ForeignKey('Watchlist')
def __unicode__(self):
return 'IndicatorPreference :'+self.User.email+"-"+self.Watchlist.name+"-"+self.indicator.group.name+"-"+self.indicator.indicator.name
I would like to add a column indicator_preferences to Watchlist model and write migration
scripts to port the existing data in IndicatorPreference model to this indicator_preferences
column. This indicator_preferences column should be a json column.
how can i do that?