Django update slug field - django

i have a problem, my model was without slug field, so now i need to add a slug field, but my models is full of data, im using django evolution and was cool added the new slug field...but how ill update the slug fiel with the data form the title field at once?
Thanks

You want this:
http://south.aeracode.org/
It will allow you to modify your schema but keep your data intact.

Related

Django model foreignkey or charfield?

Django Model and Form that I want to make
Hello. I'm fairly new to Django and I have a hard time understanding which model field to use. I didn't post any code because my code is so messy right now and most of it still following "Django for Beginner" book by William S. Vincent.
I have an app consisting 2 models, Product model and Production model. Some fields on production model are linked to product model. Then from production model, I make a form for user input using ModelForm. (There's a link to an image to help understanding model and form relation.)
I have several questions regarding this matter.
If I set product_id on Production Model as ForeignKey to Product, what field should I use for product_name and material?
When I use CharField for both product_name and material, there are entry for both product_name and material on form and I don't want that. How to change it to readonly and have value updated based on product_id? (I'm not sure if this is related to Django form or not.)
Right now I'm using ModelForm to make Production form and then using FormView to render the form. Is it the right approach? What is the difference between this one and CreateView from model?
Thank you in advance for any comments and answers.
If you have a name and a material on the product model, you don't need those on the production model unless they relate to the production object. If I were you, I'd have a foreign key on Production to the product. It might look something like;
class Production(models.Model):
product = models.ForeignKey(
to=Product,
verbose_name=_("Product"),
on_delete=models.CASCADE
)
machine = models.CharField(
verbose_name=_("Machine No"),
max_length=255
)
date = models.DateTimeField(
verbose_name=_("Date"),
blank=True,
null=True
)
Then your form might be;
class ProductionForm(forms.ModelForm):
class Meta:
model = Production
fields = (
'product',
'machine',
'date',
)
I would recommend using the django admin to get your models as you want them before you start working with views. If you know the data is being stored in a way you need, then you can worry about building the frontend. The admin is around page 70 of that book you've got. You can also do readonly fields with that.

How to get existing values from the database into the django model field for django admin

I need help to get existing questions from the field question_name into the field dependent_question (with dropdown) from the Questions model class. It will help user to select dependent question when they add any new question from django admin panel.
# model.py
class Questions(models.Model):
question_id = models.AutoField(primary_key=True)
question_name = models.CharField(max_length=300)
dependent_question = models.CharField(max_length=300,blank=True, null=True)
Take a look at this post: https://simpleisbetterthancomplex.com/tutorial/2018/01/29/how-to-implement-dependent-or-chained-dropdown-list-with-django.html
It shows you how to achieve that using javascript. I have done this, and it works great.

Django: Fields in inlineformset to display only logged in users Authors

How to create new titles for authors using inlineformset factory by selecting authors created by logged in users only.
Models.Py
Model1
user=models.ForeignKey(settings.AUTH_USER_MODEL)
Author = models.CharField(max_length=100)
Model2
Author_for_title=models.ForeignKey(Model1)
title=models.CharField(max_length=100)
Now i want to have a form where users can select the Author and type title in input field to update.
So far everything is fine. But only thing i am not able to solve is that:
How to use queryset so that Author select field display authors
created by logged in Users only?
formset=inlineformset_factory(Model1,Model2,,can_delete=True,fields=('__all__',),extra=3)
I found no answer anywhere, please help with an example.
Got the answer, thanks to documentation.
in views.py
new_formset = inlineformset_factory(Model1,Model2,can_delete=False,exclude=('user',),extra=3)
for forms in new_formset:
forms.fields['Author_for_title'].queryset = Model1.objects.filter(user=request.user)

Django model variables - Unique

I am terribly sorry for the crummy title to this question. I can't think of how to phrase it in a simple context.
I am making a simple photo gallery. The admin can create a gallery page and upload images to it. For each gallery I want there to be a single banner image from one of the photos. The Photo model has a CharField which will tell if its a banner or not. Is there a way to make the variable to allow only one 'B' for every photo that belongs to a Gallery?
You should define a unique_together variable for your class. If names of your fields are album and banner, you should add this line to Meta class of your model.
unique_together = (("album", "banner"),)
Django Documents, unique-together
And i think, using charfield for a boolean value isn't a correct choice. You should change your banner field as BooleanField.
Why don't you create a OneToOne field?
class Gallery(models.Model):
...
banner = models.OneToOneField(Photo)
this way, you have an unique relationship between a gallery and a photo.

django many2many field make not required

I created a form based on a model. The model has a many2many field. I defined the field like this:
contacts = models.ManyToManyField(Contact, blank=True, null=True)
I`m wondering now why the generated form says that this field cannot be blank. I always get the error message "This field is required.", when i do not select a contact for the contacts field.
Whats`s wrong?
In your form declaration mark this field as required=False
class MyForm(forms.ModelForm):
contacts=forms.ModelMultipleChoiceField(queryset=Contact.objects.all(),required=False)
class Meta:
model=MyModel
Possibly you did syncdb before adding blank=True, null=True?
syncdb will only create tables if they don't exist in the database. Changes to models have to be done manually in the database directly with SQL or using a migration tool such as South.
Of course, if you are still in early development, it will be easier to drop the database and run syncdb again.
Your use of null=True is confusing here. A manyToMany field results in a 3rd table relating one model to another. e.g.
Business <-> Contact
If business.contacts is empty, no records are entered into this table. null=True would make me think you are intending for NULL records to be added to this table, which doesn't seem valid.
Typically you would leave both of these attributes off.