Im trying to build an online bicycleauction and i cant seem to figure this one out.
Class Product(models.Model):
name_of_bicycle = models.Charfield()
Class Bid(models.Model):
bid_number = models.PositiveIntegerField()
name_of_bidder = models.Charfield()
bid = models.JSONField()
The JSONField should contain [name_of_bicycle, bidding amount].
Is this possible? Do i have to use JSON schema?
Related
I need to have four file fields for my model so i am using Django inline model for that. I also need to create a model for with all thode fields so that the user can fill out the form and i am using Class based views- CreateView for that. However i do not know how to get the file fields in my model form.
Models.py
class Product(models.Model)
name= models.Charfield(maxlength=50)
city= models.Charfield(maxlength=50)
state=model.Charfield(maxlength=50)
year=models.Integerfield(blank=True, null=True)
class ProductAttachment(models.Model)
attachment = models.ForeignKey(Product, related_name='attachment')
appendix_a= models.Filefield(verbose_name='Appendix A')
appendix_b= models.Filedield(verbose_name='Appendix B')
Other = models.Filefield()
Admin.py
class ProductInline(admin.StackedInline)
model=ProductAttachment
class ProductAdmin(admin.modelAdmin)
inlines= [ProductInline,]
model=Product
admin.site.register(Product, ProductAdmin)
forms.py
class ProductForm(models.form):
class Meta:
model=Product
fields='__all__'
Views.py
class ProductCreateView(CreateView):
model=Product
form_class = ProductForm
However i do not know how to get all the filefield in the form. Any help is highly appreciated. Thank you
Looking at your code the model for your ProductForm and ProductCreateView is Product, which does not contain any FileFiled. You need to create form/view for ProductAttachment as well.
I have the following model
class Entry(models.Model):
user = models.ForeignKey(User)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(default=timezone.now)
tags = TaggableManager()
I'm trying t get all the tags for a user and the following query isn't working as expected
tags = Tag.objects.filter(entry__user=u)
This is using django-taggit
https://github.com/alex/django-taggit
Looks like
tags = Tag.objects.filter(entry__user=u)
worked.. I must have had some bad code somewhere.
I have two model classes Team and Student. Each Team entity has more than one Users entities, I think "one to many" is a proper model. I code like this:
class Team(db.Model):
teamNmae = db.StringProperty(required=True)
teamID = db.IntegerProperty(required=True)
class Users(db.Model):
name = db.StringProperty(required=True)
reference = db.ReferenceProperty(Team,collection_name=teamMembers)
Follow this document, I need to first create a Team entity, then create Users whose reference is set to this Team entity. But,here is the problem, I want to create User first, then User can create a Team. So, how to set reference when I create a User that no Team exists ?
EDIT1:
I write simple code to test if I can create a entity without its reference entity created.
_author__ = 'Steven_yang'
import os
import webapp2
from google.appengine.ext import db
from google.appengine.ext.webapp import template
defaultStu = [
'qingWANG',
'stevenYANG',
'jingZHU',
'conghuiHE',
'lianDUAN',
'xinHAO'
]
class Students(db.Model):
#reference = db.ReferenceProperty(Team,collection_name='teamMember',required=False)
name = db.StringProperty(required=True)
class Team(db.Model):
teamName = db.StringProperty(required=True)
teamID = db.IntegerProperty(required=True)
def createStu():
stu_count = Students.all().count(1)
if stu_count == 0:
for stu in defaultStu:
newStu = Students(name = stu)
newStu.put()
class testReferenceHandler(webapp2.RequestHandler):
def get(self):
createStu()
self.render_page()
def render_page(self):
stus = Students.all().fetch(10)
templateValues={}
templateValues['stus'] = stus
form = os.path.join(os.path.dirname(__file__),'template/testref.html')
renderForm = template.render(form,templateValues)
self.response.out.write(renderForm)
app = webapp2.WSGIApplication([('/testref',testReferenceHandler)],debug=True)
HTML file is:
<body>
{% for s in stus%}
<div>{{s.name}}</div>
{% endfor%}
</body>
</html>
when I comment out the ReferenceProperty line, everything is fine. When I add the ReferenceProperty line, I got a server error. So, I can't create a entity without its reference created before?
Did you try to set reference to Team model as None when creating a User instance?
Also change the last line your code snippet to:
reference = db.ReferenceProperty(Team,required=False)
i.e. add required=False. This is what is suggested in this answer.
I have a course catalogue and am trying to include pre-requisits.
A course has 0 to n pre-requisits and a course can be the pre-quesit for 0 to n courses.
The course class is as follows
class Course(models.Model):
class Meta:
ordering = ['course_code', 'title']
course_id = models.AutoField(primary_key=True)
course_code = models.SlugField(max_length=20, null=False, blank=False)
....
....
class Pre_requisit(models.Model)
course = models.ForeignKey(Course, course_id')
pre_req = ???????
I have tried the pre_req field as a ForeginKey and MamyToManyField but cant find a solution.
Django will not allow 2 ForeignKeys from Pre_requisit class to the Course class.
With the ManyToMany field evn through another table I still get errors.
Can anyone let me know how to acheive this relationship.
I wish to ensure the pre-requisit course exists and be able to link to it so it can be displayed.
Many thanks.
class Course(models.Model):
....
pre_req = models.ManyToManyField("self")
read more at http://docs.djangoproject.com/en/dev/ref/models/fields/#manytomanyfield
If you wish to store extra info you can specify a "through" table also ( info at http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany) such as hard pre-requisite or recommended etc.
I get really confused with many-to-many database relationships, so can some one please clarify how I would achieve this?
I need a table of "Tags" (as in tag words) and a table for "Entries", such at many "Entries" could correspond to many Tag words.
Right now I have my models like this:
# models.py
class Tags(models.Model):
tag = models.CharField(max_length=255)
entry = models.ManyToManyField(Entry)
class Entry(models.Model):
entry = models.CharField(max_length=255)
description = models.TextField()
Now I'm confused, how would I setup my admin.py so I could then add tags when I create a new entry?
What you need is using the through feature of models:
class Tag(models.Model):
tag = models.CharField(max_length=255)
entry = models.ManyToManyField(Entry, through='TaggedEntries')
class Entry(models.Model):
entry = models.CharField(max_length=255)
description = models.TextField()
class TaggedEntries(models.Model):
entry = models.ForeignKey(Entry)
tag = models.ForeignKey(Tag)
and now use that model in your admin:
class TagsInline(admin.TabularInline):
model = TaggedEntries
extra = 1
class EntryAdmin(admin.ModelAdmin):
inlines = (TagsInline, )
admin.site.register(Entry, EntryAdmin)
admin.site.register(Tag)
You will need something along the lines of:
# admin.py
from django.contrib import admin
from models import *
class TagsInline(admin.TabularInline):
model = Tag
extra = 1
class EntryAdmin(admin.ModelAdmin):
inlines = (TagsInline, )
admin.site.register(Entry, EntryAdmin)
admin.site.register(Tag)
(Note, this code was written in a browser!)