Initializing a django modelform with data from url - django-forms

I have two models for store and city:
class City(models.Model):
name = models.CharField()
slug = models.SlugField()
class Store(models.Model):
name = models.CharField()
slug = models.SlugField()
city = models.ForeignKey(City)
If I have my Add Store url designed as
r^'mysite.com/city/(?[-\w]+)/venue/add$'
where the represents the City.slug field can I initialize a StoreForm that automatically populates the Store.city field from the url data?

In your template, define your link as follows.
{%url somename relevant_slug_text%}
Or :
href='/mysite.com/city/{{slug_text}}/venue/add'
In your url conf, define your url like:
url(r^'mysite.com/city/(?P<slug_text>\w+)/venue/add$', 'func_name', name='somename')
So, you can pass the value of relelvant_slug_text variable to your url as slug_text, and in your function definiton:
def func_name(request, slug_text):
So , you can pass text value to your funcrtion with slug_text parameter...
EDIT:
There are tow ways...
One:
Crete your city selection form using ModelForm..., then inthe second step, use posted data to populate your form again like:
form = StoreForm(request.POST)
then you can render this form to your template...
But if it is not possible o use this, yo ucan do the following...
Since you are using ModelForm to create your forms:
class StoreForm(forms.ModelForm):
# your form field definitions are here
im your function, just override city field, but at this point, since you use modelform, your form thml will be created as
<select>
<option value="id of record">"unicode value of record"</option>
So, you have record id's as option values. And you have slug_field values to initialize your field. So you have to get related city to use it...
my_city = City.objects.get(slug=<your_slug_value>)
Now you can override the form, but you must do it before you pass your form to a variable to render to your template...
StoreForm.base_fields['city'] = forms.ModelChoiceField(queryset=City.objects.all(), initial=my_city)
form = StoreForm()

Related

DRF: What's the difference between Field and Serializer

We can use serializer as a field inside another serializer..
Wonder why there's a Field class and Serializer class in DRF?
class CommentSerializer(serializers.Serializer):
user = UserSerializer()
content = serializers.CharField(max_length=200)
created = serializers.DateTimeField()
example serializer is taken from the doc https://www.django-rest-framework.org/api-guide/serializers/
As you can see, UserSerializer is much like a Field .
I'm just curious why they have serializer and field class separately..
Serilaizer is infact a field in DRF. Serializers can be nested and that is why it can be used as a field in other serializers. And yes, if you check the source code, the BaseSerializer is a subclass of Field as the serializer is just a special case of a field.
In my opinion:
In django rest framwork, you can think Serializer like a mask. It cover your origin data and change it to anything you want. Like format your json data , or validate your input data have correct format or not.
In your example,
content = serializers.CharField(max_length=200)
created = serializers.DateTimeField()
Comment have 2 direct field type CharField and DateTimeField.
user = UserSerializer()
Comment have field type is UserSerializer. This is different Serializer, and django know your CommentSerializer will have relationship with UserSerializer. And anything define in UserSerializer will use in here, for format json output or validate. And with define this nested objects, your output json will have more data like
'user': {'email': 'foobar', 'username': 'doe'}
And if you want create Comment with one user, you must pass all validate define in UserSerializer.
Conclude: in this example
Field class use for direct field.
Serializer class for relationship with other class

how to get foreign key fields using reverse lookup in python/django ajax request (json)

---models.py---
class Products(models.Model):
category = models.ForeignKey(Category)
name= models.CharField(max_length=120, unique=True)
slug = models.SlugField(unique = True)
price = models.IntegerField(default=100)
class Image(models.Model):
property = models.ForeignKey(Products, related_name='images')
image = models.ImageField(upload_to='static/images/home',blank=True,null=True)
---views.py----
if request.is_ajax():
query = Products.objects.all()
p = Paginator(query, 4)
pagedata = p.page(1)
jsondata = serializers.serialize('json', pagedata.object_list)
data = json.dumps({'id' : jsondata,})
return HttpResponse(data, content_type='application/json')
now in ajax data are in (pk, category, name, slug ,price)
but i also want foreign key field i.e 'image' using reverse lookup in ajax. i have already tried list but i want to do using reverse lookup..
You cannot use serializers.serialize and then json.dumps on your stuff. serializers.serialize is basically doing the same thing as json.dumps, to convert a python object into json string. What you need to do is manually construct a dictionary that contains all your data. You might need to loop through the Product list because there's no way to query for reverse relationships for each item in one run.

user defined form validation in grails

I want to validate three form fields.
The usual method is like this:
class User {
String name
String password
String personalInfo
static constraints = {
name(size: 4..20, unique:true, blank:false)
password(size:8..20, blank:false)
personalInfo(size: 1000, nullable:true)
}
}
but in my gsp i'll be having all three textfields in the same name
eg:
<td>Name:</td><td><g:textfield name="property"/></td>
<td>Password:</td><td><g:textfield name="property"/></td>
<td>PersonalInfo:</td><td><g:textfield name="property"/></td>
How to validate this form????
It is a really bad practice to give the same name to input fields in a form that are going to be mapped to different properties in the same domain class.
But if you cant, the fields will be sent as a list to your controller. So you can extract values in the controller like this:
def parameterList = params.property as List
def name = parameterList[0]
def password = parameterList[1]
def personalInfo = parameterList[2]
Then you can create your User object
def user = new User(name:name, personalInfo:personalInfo, password:password)
if (user.save()){ } else{ }
As the best practice you must name your fields in the form differently

Where does DropDownListFor get the model from?

You can create a dropdownlist using
#Html.DropDownListFor(model => model.SelectedId, model.DropDownItems);
but where does DropDownListFor get the value to pass into the model => model.SelectedId lambda from?
SelectedId is just the integer of the field in the model.
the model is passed in from the controller using
return View(myModel);
And the model can be defined in the view at the top
#model mymodelnamespace.RoomBookingInsert
So the dropdownbox's value is set as the SelectedId field in your model.
A proper field name for example would be RoomNo if that clarifies it better.
#Html.DropDownListFor(model => model.RoomNo, model.Rooms);
As long as model.DropDownItems (or model.Rooms in my example) Contains a item with the value of that attribute, it will set it as the selected item in the list as default.
Edit:
If you are using viewbag, rather than the model, instead use DropDownList. (each input has a for extension for use with models)
#Html.DropDownList("RoomNo",
new SelectList(ViewBag.Rooms as System.Collections.IEnumerable ?? Enumerable.Empty<SelectListItem>(), "RoomNo", "RoomName"),
new { #value = #ViewBag.RoomNo})
This above, creates an input form with id RoomNo,
and uses a SelectList that will default to empty if null. The fields that defined the values and text shown are set at the end of the select list parameters.
The last bit sets the default value to the contents of #ViewBag.RoomNo.
You can also create the select list in the controller, and then simply set the dropdown options to the viewbag.myselectList entity.

Django ModelForms: Trying to save a form using a foreign key ID

I'm trying to create a new Topic and the category id is dynamically determined in javascript on the client side. The problem i'm having is I pass the category id and I want to lookup the correct category object, but using a model form, it checks if the category is an instance of Category before I can assign it and save it.
--model.py--
class Topic(models.Model):
category = models.ForeignKey(Category)
--form.py--
class TopicForm(ModelForm):
category = forms.IntegerField(widget=forms.HiddenInput())
class Meta:
model = Topic
fields = ('category')
--view.py--
form = TopicForm(request.POST)
if form.is_valid():
form.save(commit=False) # throws exception category is not a Category instance
form.category = Category.objects.get(pk=form.cleaned_data.get('category'))
form.save()
Use a ModelChoiceField instead of the IntegerField in your form. See the built-in fields reference
Following Oggy's suggestion, I changed it to a ModelChoiceField and now Django does all the magic behind the scenes.
category = forms.ModelChoiceField(Category.objects.all(), widget=forms.HiddenInput())
Now I hope the queryset doesn't get evaluated, since it's not necessary and there are 90,000 records. :)

Resources