class Venues(models.Model):
..........
..........
category=models.ForeignKey(Category)
class Category(models.Model):
club=models.CharField()
bar=models.CharField()
adult=models.CharField()
These are the models that I have. I am a django newbie so please pardon this rather simple question. I want every Venue object to have one category that is selected from the list of categories(i.e club,bar or adult). So each category--club,bar,adult--can have many Venues but like I said every Venue has one category.
So am i at all close to what im trying to accomplish or is this totally wrong?? Please help. Thanks
Your Category could have a one field called type that type could be a string either 'club', 'bar', 'adult'. Also you could just have a Venue with a char field and give it a set of fixed choices. So as you see there are already a couple of ways to approach the way you set up your database. Can venues have more than one category? If so you might want to use a manytomany field.
I think one of the issues is right now if you wanted to add a new category you would have to manually add it in the code. If you chose a gave Category just a type field a user could add new categories from the admin without changing any code.
if you're wondering how to actually add a category to a Venue you first need to get a category instance. This can be done by retrieving one already in your db or createing a new one
category = Category.objects.get(pk=1)
next you can just create a new Venue using that category
new_venue = Venue(category=category, other_args)
new_venue.save()
will save a new venue with the category. Django documentation provides a great reference.
Related
There's a form on our website that gives you a quote if you enter a to and from address. From there, you can go ahead and make a booking or just leave the website. We save the quotes generated by users for statistic purposes (e.g compare the quote and booking ratio). Currently, I use two tables for this. Quotation and Booking. Here are the tables (models in Django):
Quotation:
to
from
price
some_charge
other_charge
Booking:
to
from
price
some_charge
other_charge
name
# other booking details
I have kept them as separate models but I have noticed they are getting very similar over time. I am adding fields to both models and also have duplicate methods. Should I make a QuotationModel and extend both from that? Any other suggestions? Any theory I should for a scenario like this?
I've got a bunch of previously created database entries of a model class I created, called Equations. I created a new type of model, EquationGroup, and I want to be able to link the existing Equations in the database to newly created EquationGroups. How would I do that?
Update:
I forgot to mention that I've got a ForeignKey relationship in Equation to EquationGroup.
Here is short version of my models.py
class EquationGroup(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Equation(models.Model):
name = models.CharField(max_length=50, null=False, blank=False)
group = models.ForeignKey(EquationGroup)
You can create a script that goes through all the equations and attaches them to equations groups. Using Equations.objects.all() you can get an iterable of all of the equations objects.
You can then go through in a for loop and assign each one to the specified Equations group.
Example:
for equation in Using Equations.objects.all():
equation.equationGroup = some_group #based on however you specify what goes in a group.
equation.save()
Do you want to make associations Equation <-> EquationGroup? If so, you should add OneToOne relation field. Look here for examples.
There is many other ways to add relations, like ManyToMany and ForeighKey.
Do you need to associate instances? In this way, you should have data to find related data and save it as OneToOne/ForeignKey.
How do you plan to update the database with the new change to your equation model? If you are using django 1.7+ you can use the built-in migrations. If not you will need to look to something like South. Thirdly you can alter the database by hand.
Anyway, the reason i bring up migrations is because you cant add a null foreign key field. When adding the field to the database table you will need to specify a default FK value, that you can go back and update using #user3154952 suggestion.
Depending on the database you are using, you may be able to remove the foreign-key constraint check temporarily. Allowing you to add the new field to the table without a dummy value, by allowing null.
This might be a little far fetched, but I want to give it a try anyway.
I want to auto populate a m2m field when the user selects a foreignkey from the same model.
For example:
venue = models.ForeignKey(Instance)
kill = models.ManyToManyField(Boss)
Now, Boss has a relation to Instance already in Instance.instance what I want is when someone selects the venue say object #1 then the m2m field will automagically update to only boss objects from that instance.
I don't even know where to start with this, google has not really helped at all. Maybe the way I'm searching for it is wrong.
i am new to both python and Django, trying to create a database that hold general information about people, so i have this:
a model for a Person (contain general info).
a model for a Category that a person belongs to (a person can be in multiple categories at the same time).
each category contain its own extra data (a person who is in "Writers" category might have some books that we want to store in DB)
i thought about model sub-classing, but it seems that this will not work if a person can be in multiple categories the same time (especially dynamically)
another thought is creating profiles that have OneToOne relationship with the Person model, but i am not sure if it is the best way
what is the best/other ways to tackle this?
It sounds like you want each category to have a ForeignKey to Person.
add ManyToManyField(Category) to Person class.
Django permissions in User model are solved the same way (via Group class)
edit:
you're right, I'm sorry about my useless answer.
My solution would looks like this:
Class Person(Model):
#property
def extras(self):
extra_data = {}
for category in self.categories.all():
category_model_class = CATEGORIES_DATA_MAP[category.name]
extra_data[category.name] = category_model_class.objects.filter(user=self.pk)
return extra_data
... where CATEGORIES_DATA_MAP is dictionary with category/model relation map
The multiple 1to1 rel is the way to go.
Define an ActorProfile model, a WriterProfile model etc, with each having a fk to User. Use some orm magic to load then when necessary.
It's basic that the FK is in the profile model, otherwise you'll need to add a new column on the user table each time you need a new kind of profile.
As the profiles will be probably somewhat overlapping, I'd suggest to use composition (instead of inheritance), do more profile types which store the common data, and keep the specific profiles for very specific data.
EDIT
Use a document-based database (mongodb?) and forget about migrations, fixed schemata, artificial joins... You have to think only about structuring the data the way you really need, but the advantages are worth considering.
I have some Django objects like this:
class Award(Model):
name = CharField(...)
date = DateTimeField(...)
class Book(Model):
name = CharField(...)
award = ForgeinKey(Award, blank=True)
(i.e. each Book may or may not have one award)
I want to make a form for adding/editing Awards. Assume lots of Books have already been created. Initially I created a ModelForm like this:
class AwardForm(ModelForm):
class Meta:
model = Award
which created a correct form with all the data, however there was no way to add books to the award (or mark this award as applicable to the books that were selected).
I was going to manually add a ModelMultipleChoiceField to the AwardForm (with a queryset for the Books), then add logic to is_valid that would check the Books that were selected, and add something to save to go through the selected Books and set the forgein key on that object to this object.
However, Is there a good 'django' way to do this automatically? i.e. just add a certain field to the Form which will do that all itself? If I were to change the Book.award to be a Award.books manytomany key it would probably do all this automatically, but I like the internal logic of having that as a forgeinkey on the Book.
I was just going to suggest using Inline's but upon re-reading your question the hard part is selecting objects that already exist and linking them to the Award that you are editing.
The way that you have suggested is the first way that I would do it, and the only other way I can think of would involve hacking away at some of the methods on your AwardAdmin to add in the functionality that you desire.
Using the ModelMultipleChoiceField though seems like quite a clean way of doing it to be honest, especially since you shouldn't really need much/any editing of the save method as the field should handle that itself?