Categories
Python Answers

How to fix CSV new-line character seen in unquoted field error in Python?

Sometimes, we want to fix CSV new-line character seen in unquoted field error in Python.

In this article, we’ll look at how to fix CSV new-line character seen in unquoted field error in Python.

How to fix CSV new-line character seen in unquoted field error in Python?

To fix CSV new-line character seen in unquoted field error in Python, we can call csv.reader with the dialect argument.

For instance, we write

file_read = csv.reader(file, dialect=csv.excel_tab)

to call csv.reader with the CSV file returned from open.

And the dialect argument is set to csv.excel_tab to read it in as a Excel tab delimited CSV.

Conclusion

To fix CSV new-line character seen in unquoted field error in Python, we can call csv.reader with the dialect argument.

Categories
Python Answers

How to override admin CSS in Python Django?

Sometimes, we want to override admin CSS in Python Django.

In this article, we’ll look at how to override admin CSS in Python Django.

How to override admin CSS in Python Django?

To override admin CSS in Python Django, we can set the css file of the Media class in the model admin class.

For instance, we write

class MyModelAdmin(admin.ModelAdmin):
    class Media:
        js = ('js/admin/my_own_admin.js',)    
        css = {
             'all': ('css/admin/my_own_admin.css',)
        }

in admin.py to add the Media class in the MyModelAdmin class.

In it, we set the css field to a dict with the all key set to the CSS file path.

Conclusion

To override admin CSS in Python Django, we can set the css file of the Media class in the model admin class.

Categories
Python Answers

How to create a self-referential foreign key with Python Django?

Sometimes, we want to create a self-referential foreign key with Python Django

In this article, we’ll look at how to create a self-referential foreign key with Python Django.

How to create a self-referential foreign key with Python Django?

To create a self-referential foreign key with Python Django, we can call ForeignKey with 'self'.

For instance, we write

class CategoryModel(models.Model):
    parent = models.ForeignKey('self')

to create the parent field in CategoryModel by setting it to models.ForeignKey('self') to create a self-referencing foreign key field parent.

Conclusion

To create a self-referential foreign key with Python Django, we can call ForeignKey with 'self'.

Categories
Python Answers

How to raise a validation error in a model’s save method in Python Django?

Sometimes, we want to raise a validation error in a model’s save method in Python Django.

In this article, we’ll look at how to raise a validation error in a model’s save method in Python Django.

How to raise a validation error in a model’s save method in Python Django?

To raise a validation error in a model’s save method in Python Django, we can use is_clean to check if the model is cleaned.

For instance, we write

class BaseModelExt(models.Model):
    is_cleaned = False

    def clean(self):
        # ...
        self.is_cleaned = True

    def save(self, *args, **kwargs):
        if not self.is_cleaned:
            self.clean()

        super().save(*args, **kwargs)

to add the clean method into our model class.

After the validation checks are run in clean, we set self.is_cleaned to True.

And then in save, we call clean is is_cleaned is False.

Finally, we call save to save the values.

Conclusion

To raise a validation error in a model’s save method in Python Django, we can use is_clean to check if the model is cleaned.

Categories
Python Answers

How to get value from form field with Python Django?

Sometimes, we want to get value from form field with Python Django.

In this article, we’ll look at how to get value from form field with Python Django.

How to get value from form field with Python Django?

To get value from form field with Python Django, we can use the form’s cleaned_data property.

For instance, we write

if myform.is_valid():
  data = myform.cleaned_data
  field = data['field']

to use the myform.cleaned_data property to get the form values as a dict.

Conclusion

To get value from form field with Python Django, we can use the form’s cleaned_data property.