hey i tried this code but still have an error that is Not Null constraint failed i know this is because of my cheque_no that is unique type but how could i remove this error i did not remove my cheque_no is unique because its required.now am getting this problem i want to save these entries in my model.
views.py
#csrf_exempt
def jsdata(request):
table_data = json.loads(request.POST.get('MyData'))
# print(table_data)
r_data = {
'success': True,
}
for data in table_data:
# Since you are just creating objects you don't need to save created object in a variable.
Mvouchar.objects.create(bill_no = data['BillNo'], bill_details=data['BillDetails'],am=data['Amount'])
# r_data['success'] = False
# IMO Views responding to ajax requests should send JsonResponse
if r_data['success']:
r_data['msg'] = 'Data Saved'
else:
r_data['msg'] = 'Not all Data Saved'
return JsonResponse(r_data)
models.py
class Mvouchar(models.Model):
related = models.ForeignKey(Signs, on_delete=models.CASCADE, null=True, blank=True)
bill_no = models.CharField(max_length=8000, null=True, blank=True)
bill_details = models.CharField(max_length=10000, null=True, blank=True)
am = models.CharField(max_length=30000, null=True, blank=True)
cheque_no = models.PositiveIntegerField(validators=[MaxValueValidator(6)], unique=True, help_text='integers only')
def __str__(self):
if self.related:
return self.related.relation.username.title()
else:
return 'no related!'
class Meta:
verbose_name_plural = "Single Cheque Multiple Vouchar Of Users"
javascript
$("#btnjson").click(function () {
var array1 = [];
$("tbody tr").each(function () {
var firstTableData = {};
firstTableData.BillNo = $(this).find('td').eq(0).text();
firstTableData.BillDetails = $(this).find('td').eq(1).text();
firstTableData.Amount = $(this).find('td').eq(2).text();
array1.push(firstTableData);
//}
});
alert(JSON.stringify(array1));
$.ajax({
type: "POST",
url: "/jsondata/",
dataType: 'json',
data: {MyData: JSON.stringify(array1)},
success: function(msg){
alert(msg);
}
});
return false;
} );
});
from django.core.validators import RegexValidator
CHEQUE_REGEX = RegexValidator(
regex=r'^\d{6}$',
message="Cheque Number must be exactly 6 digits"
)
class Mvouchar(models.Model):
...
cheque_no = models.CharField(validators=[CHEQUE_REGEX, ], unique=True, max_length=6, help_text='integers only')
...
If you read the second traceback (The first traceback is from a different view views.mvoucha, and you didn't include that code in the question), you'll find that the problem is this line in your view function.
table_data = json.loads(request.POST.get('MyData'))
What's happening is that request.POST.get('MyData') returns None, which can't be represented as json. That's why the json encoder raises a TypeError.
The request.POST dictionary is used with form data. When you want to parse a json payload, you have to use request.body instead. For example like this:
table_data = json.loads(request.body).get('MyData')
https://docs.djangoproject.com/en/2.1/ref/request-response/#django.http.HttpRequest.POST
Related
I’m trying to build a web app using Django and I want to upload a file (a pdf or word document) using ajax (the ajax module in jquery).
I’m building my API using the django-rest-framework.
I’ve pretty much tried to do what every site says but can’t seem to get around it.
Has anyone done this?
My views:
def contract_post(request):
contract = Contract()
if request.method == 'POST':
serializer = ContractSerializer(contract, data=request.data)
data = {}
if serializer.is_valid():
serializer.create(validated_data=request.data)
handle_uploaded_file(request.FILES['atasament'])
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def handle_uploaded_file(f):
with open(f.name, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
My serializer (it's a nested serializer):
class ContractSerializer(serializers.ModelSerializer):
client = ClientSerializer(read_only=False)
termen = serializers.DateField(format='%Y-%m-%d', default=datetime.date.today)
atasament = serializers.FileField(max_length=None, allow_empty_file=True)
class Meta:
model = Contract
fields = (
'pk',
'tip',
'nr_data',
'obiectul',
'termen',
'atasament',
'client',
)
def create(self, validated_data):
client = validated_data.pop('client')
client_instance, created = Client.objects.get_or_create(**client)
contract_instance = Contract.objects.create(**validated_data, client=client_instance)
return client_instance
My model:
class Contract(models.Model):
TIPURI = (
('P', 'Proiectare'),
('E', 'Executie'),
('D', 'Documentatie'),
)
tip = models.CharField(max_length=300, choices=TIPURI)
nr_data = models.CharField(max_length=50)
obiectul = models.TextField()
termen = models.DateField(default=datetime.date.today)
atasament = models.FileField(upload_to='main/documents/', blank=True, null=True)
client = models.ForeignKey(Client, on_delete=models.SET_NULL, null=True)
def __str__(self):
return self.nr_data
class Meta:
verbose_name_plural = "Contracte"
My JS:
$('#form_add').submit(function (event) {
event.preventDefault();
let input_atasament = document.getElementById('input_atasament');
let client_pk = $("#input_clienti").val();
let nr_data = "test";
let tip = $("#input_tip").val();
let obiectul = $("#input_obiectul").val();
let termen = $("#input_termen").val();
const url = \/api/contract-post/`;let client_url = `/api/client-get/${client_pk}`;let client = $.ajax({url: client_url,async: false,dataType: 'json'}).responseJSON;`
let data = new FormData();
data.append('nr_data', "test");
data.append('tip', tip);
data.append('obiectul', obiectul);
data.append('termen', termen);
data.append('atasament', input_atasament.files[0]);
data.append('client', JSON.stringify(client));
var action = function(d) {
console.log(d);
};
$.ajax({
url: url,
headers: {
'X-CSRF-TOKEN': csrftoken,
'Content-Type': 'multipart/form-data',
},
data: data,
type: "POST",
contentType: false,
processData: false,
success: action,
error: action
});
});
And what error I get now:
"Multipart form parse error - Invalid boundary in multipart: None"
Im using angular/typescript where I upload a form of data to my Django REST API. With the data all is correct, because I can log my whole form and I get back all my data. But when it comes to upload the data I get this error:
{src: ["No file was submitted."], tag: ["This field is required."]}
so somehow it does not recognize my data because Im actually submitting data.
frontend code
upload.service
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'multipart/form-data'})
};
...
uploadPost(src: File, tag: string) {
return this.http.post<any>(`{this.url}/posts/`, {src, tag}, httpOptions);
}
post.page
ngOnInit() {
this.storage.get('image_data').then((imageFile) => {
console.log(imageFile)
this.categoryForm.patchValue({
'image': this.storage.get('image_data')
});
});
this.categoryForm = new FormGroup({
'category': new FormControl('', Validators.compose([
Validators.maxLength(25),
Validators.minLength(1),
Validators.required
])),
'image': new FormControl(null),
});
apiSubmit() {
console.log('logged')
console.log(this.f.image);
this.submitted = true;
if (this.categoryForm.invalid) {
return;
}
this.isLoading = true;
this.loadingEl.present();
this.uploadService.uploadPost(
this.f.image,
this.f.category
)
.pipe(tap(x => this.loadingEl.dismiss())
)
.subscribe(
data => {
this.router.navigate(['one']);
},
error => {
this.error = error;
}
);
}
Django:
models.py
class Post(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='posts', on_delete=models.CASCADE)
src = models.ImageField(blank=False, null=False, editable=False, upload_to=utils.get_file_path,)
date_posted = models.DateTimeField(auto_now_add=True, editable=False)
last_modified = models.DateTimeField(auto_now=True)
when = models.FloatField(blank=True, null=True)
lock = models.BooleanField(default=False)
des = models.CharField(
max_length=100,
validators=[
RegexValidator(
regex='^[-_a-zA-Z0-9\.]+$',
message='only 1 to 20 characters and only letters, numbers, point, minus and underscore are allowed',
),
])
view.py
#swagger_auto_schema(request_body=doc_serializer.CreatePostDummy, responses={201: PostSerializer})
def create(self, request, *args, **kwargs):
"""permission required: authenticated. Note: the schema is not json it's multipart"""
return super().create(request, *args, **kwargs)
doc_serializers.py
class CreatePostDummy(serializers.HyperlinkedModelSerializer):
`enter code here`user = serializers.HyperlinkedRelatedField(required=False, read_only=True, view_name='user-detail')
src = serializers.CharField(required=True, help_text='Not Char but image upload field')
I've tried all possible solutions on several threads and I'm still unable to fix the problem. I have the following code:
models.py
class CustomerVisit(models.Model):
start_date = models.DateField()
end_date = models.DateField()
customer = models.ForeignKey(Customer)
address = models.ForeignKey(Address)
forms.py
address = forms.ModelChoiceField(label='Address',
queryset=Address.objects.none(),
widget=forms.Select(attrs={'style': 'width: 100%;'}))
customer = forms.ModelChoiceField(label='Customer',
queryset=Customer.objects.all(),
widget=forms.Select(attrs={'style': 'width: 100%;'}))
views.py
if request.method == "POST":
# Cleaning fields
post = request.POST.copy()
post['address'] = Address.objects.get(id=post['address'])
post['start_date'] = dateparser.parse(post['start_date'])
post['end_date'] = dateparser.parse(post['end_date'])
# Updating request.POST
request.POST = post
form = CustomerVisitForm(request.POST)
if form.is_valid():
form.save(commit=True)
return redirect("customervisit:calendar")
js
$("#id_customer").select2({}).on("change", function () {
var customer_id = $("#id_customer").val();
var id_address = $("#id_address");
id_address.select2({
ajax: {
url: '/get_customer_address/' + customer_id,
dataType: "json",
type: "GET",
data: function (params) {
var queryParameters = {
term: params.term
}
return queryParameters;
},
processResults: function (data) {
return {
results: $.map(data, function (item) {
return {
text: item.text,
id: item.id
}
})
};
}
}
});
});
My address select its being populated based on customer selection using ajax call using select2. After reading several threads I noticed that modelchoicefield expects a Address object so that's why I'm using the following code on my view before the form is being validated: post['address'] = Address.objects.get(id=post['address']) but I'm still getting the Select a valid choice. That choice is not one of the available choices. error
I'm using queryset=Address.objects.none(), because I need an empty select
Problem solved.
If someone in the future have the same error as me, checking to_python method from the ModelChoiceField saved my day:
def to_python(self, value):
if value in self.empty_values:
return None
try:
key = self.to_field_name or 'pk'
value = self.queryset.get(**{key: value})
except (ValueError, TypeError, self.queryset.model.DoesNotExist):
raise ValidationError(self.error_messages['invalid_choice'], code='invalid_choice')
return value
So I changed my queryset to queryset=Address.objects instead of queryset=Address.objects.none() or queryset=Address.objects.all()
Thanks Daniel Roseman for your comments
I know what you're trying to achieve, my solution would be to initialise the field as charfield with widget select, and then override the clean method. Example:
class InvoiceForm(forms.ModelForm):
customer = forms.CharField(widget=forms.Select(attrs={'style': 'min-width: 150px;'}))
class Meta:
model = Invoice
exclude = ['id', 'created', 'is_paid']
def clean_customer(self):
customer_id = self.cleaned_data['customer']
customer = Customer.objects.get(id=customer_id)
if customer:
return customer
else:
raise ValidationError("Customer ID invalid")
I want to return only the clandpin fields. I have this model below:
model.py
class ButuanMaps(gismodel.Model):
class Meta:
verbose_name = u'Butuan Map'
verbose_name_plural = u'Butuan Maps'
clandpin = gismodel.CharField("Land PIN", max_length=50, null=True, blank=True)
ssectionid = gismodel.ForeignKey(Section)
#ssectionid_id = gismodel.IntegerField()
geom = gismodel.MultiPolygonField("Geom ID", srid=32651, null=True, blank=True)
objects = gismodel.GeoManager()
def __unicode__(self):
return self.clandpin
In my template, here's the code for AJAX:
<script type="text/javascript">
$(document).ready(function() {
$("#formsection").change(function() {
$.ajax({
url : "/sectionpins",
type : "GET",
dataType: "html",
data : {
'sectionid' : $('#formsection').val(),
'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val()
},
success : searchSuccess,
error: errs
});
return false;
});
});
function searchSuccess(data){
alert("Success");
$('#forminput').append(
"<option value='"+data+"'>"+data+"</option>"
);
}
function errs(){
alert('Error!!!!');
}
</script>
In views.py:
def section_landpins(request):
if request.method == "GET":
m = ButuanMaps.objects.filter(ssectionid=request.GET['sectionid'])
landpins = serializers.serialize("json", m.values('clandpin'), fields=("clandpin"))
data = json.dumps({
'pins': landpins,
})
return HttpResponse(data, content_type='application/json')
It returned an error
AttributeError at /sectionpins'dict' object has no attribute '_meta'
When I changed the query with this:
m = ButuanMaps.objects.filter(ssectionid=request.GET['sectionid'])
It doesn't return an error but this:
{"pins": "[{\"pk\": 185625, \"model\": \"tbl.butuanmaps\", \"fields\": {\"clandpin\": \"162-12-0001-055-37\"}}
You can do this by requesting only the field you are interested in from the ORM, then evaluating the ValueQuerySet into a list of dictionaries:
section_id = request.GET.get('sectionid', 'defaultid')
m = ButuanMaps.objects.filter(ssectionid=section_id).values('landpins')
return HttpResponse(json.dumps({'pins': list(m)}),
content_type='application/json')
I send data from html fields via AJAX
var dataToSend = { some1 : $('.name1').html().replace(/[\n\r]/g, '').trim(),
some2 : $('.name2').html().replace(/[\n\r]/g, '').trim(),
some3 : $('.name3').html().replace(/[\n\r]/g, '').trim(),
some4 : $('.name4').html().replace(/[\n\r]/g, '').trim() }
if (dataToSend) {
$.ajax({
url: 'do/smth',
data: dataToSend,
async: false,
type: 'POST',
success: function() {
alert('draft is saved')
}
});
}
Then i receive POST request which does contain data but my model saves only when all arguments are existing, so for example if i get only {some1:"value1",some2:"value2"} my django view WONT save it.
The question is: how can i possibly save data into db, if my fields are empty, as you can see i have blank=True in my model, but it does not help
model
class Draft(models.Model):
name = models.CharField(_('company name'), max_length=100, blank=True)
slug = models.SlugField(max_length=255, blank=True)
pitch = models.CharField(_('pitch'), max_length=140, blank=True)
url = models.CharField(_('url'), max_length=255, null=True, blank=True)
thesis = models.TextField(_('thesis'), blank=True)
def __unicode__(self):
return self.name
views
def save_from_ajax_post(request):
data = dict(request.POST.items())
draft = Draft.objects.create(data)
draft.save()
return HttpResponse(json.dumps({'status': 1}), content_type="application/json")