I have something similar to the example below
from django.db import models
from django.contrib.auth.models import User
class a(models.Model):
user = models.ForeignKey(User)
class b(a):
value = models.IntegerField()
class c(a):
model_id = models.IntegerField()
I have a User instance assigned to variable current_user, is there any way I can make current_user.b_set work? Thank you for reading.
The FK is on a, not b, so there is no "b_set". You can query User for objects that have an a that have a b, but a_set is the only property you have.
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.
Welcome,
I have some problem with limiting choices related with ForeignKey. Below I'm attaching fragment of my code (models.py):
class Car(models.Model):
name = models.CharField(max_length=50)
....
class Driver(models.Model):
name = models.CharField(max_length=50)
car = models.ForeignKey(Car)
....
class CarForm(ModelForm):
class Meta:
model = Car
class DriverForm(ModelForm):
def __init__(self, *args, **kwargs):
super (DriverForm,self).__init__(*args, **kwargs)
self.fileds['car'].queryset = Car.objects.filter(???_1_???)
class Meta:
model = Driver
Could anybody give me some advices how should be defined ???1??? to restrict available Car objects only to these which aren't assign to any Driver?
First of all, you may want to consider changing the relationship between Car and Driver to a OneToOneField rather than a ForeignKey if each Car can always only have a single Driver.
However, if you just want to restrict the choices in the form, your queryset needs to be something like:
from django.db.models import Count
self.fields['car'].queryset = Car.objects.annotate(num_drivers=Count('driver')).filter(num_drivers=0)
Hi I have a lot of users in my system who are classified into different types. I want to store the address details of all those users. For instance the user could be a student, a school or a franchisee. All the users here could have an address information associated with them.
from django.db import models
from django.contrib.auth.Models import User
class Address(models.Model):
user = models.OneToOneField(User)
address = models.TextField()
class Student(models.Model):
user_id = models.ForeignKey(User)
address = models.ForeignKey(Address)
class School(models.Model):
user_id = models.ForeignKey(User)
address = models.ForeignKey(Address)
contact_person_name = models.CharField(max_length=50)
In this scenario there are 2 references to the User model - one through user_id and another through address.user though they should be referring to the same instance. Is it a bad practice to have duplicate references?
I thought of leaving out the 'user' foreignkey in Address, but I think that the address can't exist without a user. How to better model this?
As you already mentioned in question duplication of same field in
a model is not a good Idea.
If these are your actual models, I would suggest you using abstract
models:
from django.db import models
from django.contrib.auth.Models import User
class Profile(models.Model):
user = models.OneToOneField(User, related_name="%(app_label)s_%(class)s_related")
address = models.TextField()
class Meta:
abstract = True
class Student(Profile):
pass
class School(Profile):
contact_person_name = models.CharField(max_length=50)
This will generate two tables: students, schools with fields
user, address and user, address, contact_person_name
respectively.
I have the following
class Employee(User):
emplorateID=models.OneToOneField(Code)
business=models.ForeignKey(Business)
but I have existing Users that I'd like to select in the EmployeeAdmin and enter the additional field data for. I don't see how I would customize the ModelAdmin to handle creating a new Employee from an existing User (effectively a new row in the Employee table for referencing the existing User entry)
Inheritance isn't what you want in this case; you just need another model with a OneToOneField pointing back.
Try this:
from django.contrib.auth.models import User
from django.db import models
class Employee(models.Model):
user = models.OneToOneField(User, related_name='employee')
emplorateID = models.OneToOneField(Code)
business = models.ForeignKey(Business)
It should JustWork™.
I don't know the form answer, but here is the code answer:
employee = Employee(user_ptr=existing_user,
emplorateID=something,
business=somebusiness)
employee.save_base(raw=True)
In my application I am adding a simple message service that allows a user to send out a message to a subset of other users on the system:
from django.db import models
from django.contrib.auth.models import User
class Message(models.Model):
timestamp = models.DateTimeField()
sender = models.ForeignKey(User)
recipients = models.ManyToManyField(User)
text = models.CharField(max_length=1000)
def __unicode__(self):
return '%s %s: %s' (self.sender.first_name,self.sender.last_name,self.timestamp)
class Meta:
ordering = ('timestamp')
However, I would like to record if each user has read the message, so would need to add a boolean field to the table holding the many-to-many relationship.
Should I do this by explicitly adding another model and using ForeignKey() only, or is there a more 'django' way of doing this?
Thanks
Never mind, my googling power were weak, just found the right place in the documentation:
http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships